Pie*_*ter 7 exc-bad-access core-data objective-c ios
我的应用程序有时会在完全加载到以下行之前崩溃:
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
Run Code Online (Sandbox Code Playgroud)
这个if状态所在的完整方法如下(我认为这是非常标准的):
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil) {
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData.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;
}
Run Code Online (Sandbox Code Playgroud)
更新
第二次-(NSPersistentStoreCoordinator *)persistentStoreCoordinator调用该方法时发生崩溃.编辑
从第一个可见的viewController调用它的第一次:
- (void)updateStats {
NSLog(@"Updating stats");
dispatch_queue_t request_queue = dispatch_queue_create("updateNumberOfSchedules", NULL);
dispatch_async(request_queue, ^{
AppDelegate *theDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:[theDelegate persistentStoreCoordinator]];
...
});
}
Run Code Online (Sandbox Code Playgroud)
第二次(当我的DeviceLinker类要在checkInactiveLinks方法中检查数据库中的非活动链接时,有时会发生崩溃.此方法在启动时调用applicationDidBecomeActive:
-(void) checkInactiveLinks {
AppDelegate *theDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *newMoc = [[NSManagedObjectContext alloc] init];
[newMoc setPersistentStoreCoordinator:[theDelegate persistentStoreCoordinator]];
...
}
Run Code Online (Sandbox Code Playgroud)
纠正我如果我错了但是读了我的代码我会认为第二次调用persistentStoreCoordinator getter它应该返回__persistentStoreCoordinator并且不分配并初始化一个新的...
更新2 在if语句的同一行上我也经常得到这个:
-[__NSCFDictionary _hasPrecomputedKeyOrder]: unrecognized selector sent to instance 0x7dd3770
Run Code Online (Sandbox Code Playgroud)
更新3
我编辑了我的构建方案,并在诊断选项卡下打开了僵尸和日志异常.现在我明白了-[NSPersistentStoreCoordinator unlock]: message sent to deallocated instance 0x8916090.请注意,我的代码中没有任何显式锁.
Jod*_*ins 19
看起来您正在同时从多个线程进入该段代码.当你进行延迟实例化时,你必须确保只通过一个执行线程的"第一次"将同时通过.您可以使用以下策略在主线程上同步对它的访问.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil) {
return __persistentStoreCoordinator;
}
// Add this block of code. Basically, it forces all threads that reach this
// code to be processed in an ordered manner on the main thread. The first
// one will initialize the data, and the rest will just return with that
// data. However, it ensures the creation is not attempted multiple times.
if (![NSThread currentThread].isMainThread) {
dispatch_sync(dispatch_get_main_queue(), ^{
(void)[self persistentStoreCoordinator];
});
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData.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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3878 次 |
| 最近记录: |