调用addpersistentstorewithtype

时间:2018-09-18 21:49:35

标签: ios objective-c core-data crash core-data-migration

我正在使用下面的代码创建一个NSPersistentStore,但由于下面发布的错误而崩溃。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
}

// Check if we need to copy initial database store
NSString *storePath = [[self applicationDocumentsDirectory]
                       stringByAppendingPathComponent: PersistentStoreFilename];
//[self checkPersistentStore:storePath];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
// Enable automatic lightweight migration
NSDictionary *options = @{
                          NSMigratePersistentStoresAutomaticallyOption : @YES,
                          NSInferMappingModelAutomaticallyOption : @YES
                          };
NSError *error = nil;
NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
NSLog(@"fileAttributes ---- %@",fileAttributes);
NSLog(@"before file attributes - storePath ---- %@",storePath);
[[NSFileManager defaultManager] setAttributes:fileAttributes ofItemAtPath:storePath error:&error];

persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                              initWithManagedObjectModel:[self managedObjectModel]];

NSLog(@"1.persistentStoreCoordinator ---- %@",persistentStoreCoordinator);
NSLog(@"2.applicationDocumentsDirectory ---- %@",[self applicationDocumentsDirectory]);
NSLog(@"3.PersistentStoreFilename ---- %@",PersistentStoreFilename);
NSLog(@"4.storePath ---- %@",storePath);
NSLog(@"4.storeUrl ---- %@",storeUrl);

NSLog(@"5)persistentStoreCoordinator ---- Error loading core data database");

if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                             configuration:nil URL:storeUrl options:options error:&error]) {
    /*Error for store creation should be handled in here*/

    // REA - removing current store and replacing with one from bundle
    //        NSFileManager *fileManager = [NSFileManager defaultManager];
    //        [fileManager removeItemAtPath:storePath error:nil];
    //        [self checkPersistentStore:storePath];
    NSLog(@"First attempt addPersistentStoreWithType error %@, %@", error, [error userInfo]);
    if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                 configuration:nil URL:storeUrl options:options error:&error]) {
        // Even the bundle version isn't loading - something really bad happened and we can't run
        NSLog(@"Second attempt addPersistentStoreWithType error %@, %@", error, [error userInfo]);
        NSLog(@"6)persistentStoreCoordinator ---- Error loading core data database");
        abort();
    }
}

return persistentStoreCoordinator;
}

这会导致崩溃,下面是设备中的nslog

  

在文件属性之前-storePath ---- /var/mobile/Containers/Data/Application/AA4EF2AA-1E72-4BC9-A939-732A1BB65E71/Documents/iPhoneCoreData.sqlite

     

1.persistentStoreCoordinator ----

     

2.applicationDocumentsDirectory ---- / / var / mobile / Containers / Data / Application / AA4EF2AA-1E72-4BC9-A939-732A1BB65E71 / Documents

     

3.PersistentStoreFilename ---- iPhoneCoreData.sqlite

     

4.storePath ---- /var/mobile/Containers/Data/Application/AA4EF2AA-1E72-4BC9-A939-732A1BB65E71/Documents/iPhoneCoreData.sqlite

     

4.storeUrl ---- file:///var/mobile/Containers/Data/Application/AA4EF2AA-1E72-4BC9-A939-732A1BB65E71/Documents/iPhoneCoreData.sqlite

崩溃日志:

异常类型:EXC_CRASH(SIGKILL) 异常代码:0x0000000000000000、0x0000000000000000 异常说明:EXC_CORPSE_NOTIFY 终止原因:命名空间SPRINGBOARD,代码0x8badf00d 终止描述:SPRINGBOARD,场景创建看门狗越界:com.united.UnitedAgentMobileApp用尽了19.27秒的实际(墙上时钟)时间余量| | ProcessVisibility:前景| ProcessState:运行| WatchdogEvent:场景创建|看门狗可见性:前景| WatchdogCPUStatistics:(|“经过的总CPU时间(秒):21.220(用户21.220,系统0.000),53%CPU”,|“经过的应用程序CPU时间(秒):19.319,48%CPU” |) 由线程触发:0

回溯:

线程0名称:调度队列:UnitediPhoneCoreData.sqlite的SQLQueue 0x117dc0080 线程0崩溃: 0 libcompression.dylib 0x00000001841865d8 zlibDecodeBufferSafe + 40 1 libcompression.dylib 0x000000018418747c zlib_decode_stream_process + 288 2 libcompression.dylib 0x000000018418747c zlib_decode_stream_process + 288 3 CoreData 0x000000018530d92c-[NSSQLiteConnection _decompressedModelWithData:] + 188 4 CoreData 0x000000018530dc5c-[NSSQLiteConnection fetchCachedModel] + 552 5 CoreData 0x00000001852e18b0 __34- [NSSQLCore cachedModelWithError:] _ block_invoke + 112 6 CoreData 0x00000001852301d4-[NSSQLStoreRequestContext executeRequestUsingConnection:] + 248 7 CoreData 0x00000001852b2118 __52- [NSSQLDefaultConnectionManager handleStoreRequest:] _ block_invoke + 84 8 libdispatch.dylib 0x000000018239aa14 _dispatch_client_callout + 16 9 libdispatch.dylib 0x00000001823a3618 _dispatch_queue_barrier_sync_invoke_and_complete + 56 10 CoreData 0x00000001852b2014-[NSSQLDefaultConnectionManager handleStoreRequest:] + 256 11 CoreData 0x00000001853a2524-[NSSQLCoreDispatchManager routeStoreRequest:] + 264 12 CoreData 0x00000001852e3fdc-[NSSQLCore dispatchRequest:withRetries:] + 236 13 CoreData 0x00000001852e1738-[NSSQLCore cachedModelWithError:] + 112 14 CoreData 0x00000001852e297c + [NSSQLCore cachedModelForPersistentStoreWithURL:options:error:] + 688 15 CoreData 0x00000001853363ac-[NSStoreMigrationPolicy sourceModelForStoreAtURL:metadata:error:] + 1256 16 CoreData 0x00000001853379c8-[NSStoreMigrationPolicy + 1653192(InternalMethods)_gatherDataAndPerformMigration:] + 772 17 CoreData 0x00000001852bba34 __91- [NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:] _ block_invoke + 2896 18 CoreData 0x00000001852c956c gutsOfBlockToNSPersistentStoreCoordinatorPerform + 212 19 libdispatch.dylib 0x000000018239aa14 _dispatch_client_callout + 16 20 libdispatch.dylib 0x00000001823a3618 _dispatch_queue_barrier_sync_invoke_and_complete + 56 21 CoreData 0x00000001852b7944 _perform + 200 22 CoreData 0x00000001851adfa4-[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:] + 384

1 个答案:

答案 0 :(得分:0)

因此,我们遵循断路器模式,并遵循下面的博客文章来解决此问题。

https://code.tutsplus.com/tutorials/core-data-from-scratch-migrations--cms-21844