在我的应用程序中,我正在使用CoreData来保存数据,而且我正在从服务器推送和提取数据。我还使用相同的帐户在多个设备之间同步数据。
但是最近,我从crashLytics报告中得到了这个崩溃。
This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation
所以在我的下面的函数中,我正在调用" saveContext"这种方法。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//Creating a new instance of an managed object.
Entity *cycle = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:appDelegate.managedObjectContext];
[cycle setEmailID:m_emailId];
[cycle setStartDate:dateFromServer];
[cycle setEndDate:dateFromServer];
[appDelegate saveContext];
}
//appDelegate.m
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil)
{
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"LoveCycles.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
*/
//NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
//abort();
}
return _persistentStoreCoordinator;
}
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
{
*/
// NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
//abort();
}
}
}
创建ManagedObject Context(MOC)
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil)
{
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
所以我几乎没有怀疑。
1)我是否应该每次都拨打"[appDelegate saveContext]"
,从服务器接收数据,或者只保存在应用程序的退出处。
2)CrashLytics报告指向"[appDelegate saveContext]"
。这也发生在iOS 9上。
3)我无法在运行iOS 9的设备上复制此问题。因此,不了解用户是否实际看到此崩溃,以及此错误的后果是什么。
答案 0 :(得分:0)
你需要将ManagedObjectContext与persistentStore关联起来,就像这样(Swift代码):
lazy var managedObjectContext: NSManagedObjectContext? = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
if coordinator == nil {
FLOG(5, message: " Error getting managedObjectContext because persistentStoreCoordinator is nil")
return nil
}
var managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
// Set the MergePolicy to prioritise external inputs
let mergePolicy = NSMergePolicy(mergeType:NSMergePolicyType.MergeByPropertyStoreTrumpMergePolicyType )
managedObjectContext.mergePolicy = mergePolicy
return managedObjectContext
}()