Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS Swift中处理CoreData崩溃?_Ios_Iphone_Swift_Crash - Fatal编程技术网

如何在iOS Swift中处理CoreData崩溃?

如何在iOS Swift中处理CoreData崩溃?,ios,iphone,swift,crash,Ios,Iphone,Swift,Crash,我需要处理一个核心数据崩溃。我的代码在managedObjectContext.save()上崩溃。 但catch块并没有捕获任何异常。为了避免崩溃,如何更好地编写Catch块这是我的代码 do { try managedObjectContext.save() } catch let error as NSError { Print.print("Error saving data store: \(error)") }

我需要处理一个核心数据崩溃。我的代码在managedObjectContext.save()上崩溃。 但catch块并没有捕获任何异常。为了避免崩溃,如何更好地编写Catch块这是我的代码

do {
      try managedObjectContext.save()              
   } 
catch let error as NSError {
      Print.print("Error saving data store: \(error)")
     }

这是使用CoreData保存数据的示例。这可能会对您有所帮助

 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        if let entity = NSEntityDescription.entity(forEntityName: "Employees", in: context){
           let myItem = NSManagedObject(entity: entity, insertInto: context)
            myItem.setValue(nameTF.text, forKey: "names")
            myItem.setValue(15655, forKey: "mobileNo")

            do {
                try context.save()

            }catch let nserror as NSError{
                print("ERROR: Coredata error \(nserror)")
            }

        }

这看起来真的把上下文、持久存储及其协调器的初始化搞砸了。你最好调查一下。类似的帖子已经出现在网上了

更重要的是,您不会使用Swift中的
try catch
截获此类异常。实际上,Swift
try-catch
与异常无关,但它是一个高级API,可用于拦截报告的错误。在您的情况下,您只需截获在将数据保存到数据库时可能报告的错误。但错误似乎来自更深一层


更进一步说,整个核心数据仍然完全在objectiveC中,objectiveC具有完全不同的抛出异常的系统,尽管这些异常可能会被objectiveC
try catch
截获,但相同的异常不会被Swift截获。此系统所做的只是将指向错误的指针输入Objective中使用的方法:
.save(&error)
。只有当此错误为非空时,才会触发catch块。

这可能是内存问题。您正在保存多少数据?什么是崩溃?在这里发布…如何使用此代码预测崩溃。在此处添加崩溃日志print(“错误保存数据存储:(错误)”)您必须像这样使用print语句。您可以更改print语句并再次运行它吗。0 CoreFoundation 0x191c76fe0_uuuexception预处理+124(NSException.m:165)1 libobjc.A.dylib 0x1906d8538 objc_exception_throw+56(objc exception.mm:521)2 CoreData 0x19402d828-[NSPersistentStoreCoordinator\u coordinator\u您从未成功打开过\u数据库\u磁盘\u已满:+56(NSPersistentStoreCoordinator.m:2616)3 CoreData 0x19402d930-[NSPersistentStoreCoordinator\u InterspectErrorAndRow]+208(NSPersistentStoreCoordinator.m:2646)我的错误没有被捕获到catch块中。如何为异常写入多个捕获?错误您将在捕获块中获得无需写入多个捕获块。请确保您在代码数据中获得错误。