我最近在使用临时NSManagedObjectIDs
引起的应用程序中遇到了一个问题。为响应网络请求,映射操作将受管对象插入到专用队列MOC中,然后将其一直保存到根MOC(分配了persistentStoreCoordinator
的那个)。
我们知道不会获得永久身份证可能会导致问题,但我们没有遇到任何问题,所以我们坚持使用它。
我最后偶然发现了一个临时ID的问题,并让切换到获取永久ID。要明确:每次我们在映射操作中插入新对象时,我们都会获得永久ID。
问题解决了,快乐的日子!不是那么快......在iOS 7.1(8.4工作正常)下,对NSPersistentStoreCoordinator removePersistentStore:
的调用现在无限期地挂起。当用户注销(清除所有数据)时,我们会将此作为Core Data堆栈拆卸的一部分。
这是我们的持久性管理器中的相关代码:
- (void)reset {
FCYWeakifySelf;
[self.managedObjectContext performBlockAndWait:^{
FCYStrongifySelfInto(strongSelf);
strongSelf.managedObjectContext = nil;
strongSelf.persistentStoreManagedObjectContext = nil;
for (NSPersistentStore *store in strongSelf.persistentStoreCoordinator.persistentStores) {
NSError *error = nil;
BOOL didRemoveStore = [strongSelf.persistentStoreCoordinator removePersistentStore:store error:&error];
FCYAssert(didRemoveStore, @"Failed to remove persistent store: %@\n%@", [error localizedDescription], [error userInfo]);
}
strongSelf.persistentStoreCoordinator = nil;
strongSelf.managedObjectModel = nil;
}];
[self initializeCoreData];
[self notifyDelegates];
}
- (void)initializeCoreData {
FCYAssert(!self.managedObjectModel, @"CoreData has already been initialized");
NSBundle *bundle = [NSBundle bundleForClass:[TTPersistenceManager class]];
self.managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:@[bundle]];
self.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
NSError *error = nil;
NSPersistentStore *store = [self.persistentStoreCoordinator addPersistentStoreWithType:[self storageType] configuration:nil URL:nil options:nil error:&error];
FCYAssert(store != nil, @"Failed create persistent store: %@\n%@", [error localizedDescription], [error userInfo]);
self.persistentStoreManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
self.persistentStoreManagedObjectContext.persistentStoreCoordinator = self.persistentStoreCoordinator;
self.persistentStoreManagedObjectContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy;
self.managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
self.managedObjectContext.parentContext = self.persistentStoreManagedObjectContext;
self.managedObjectContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy;
}
当应用程序挂起并且我停止执行时,这是回溯:
* thread #1: tid = 0x38ee8e, 0x0000000112f6551a libsystem_kernel.dylib`semaphore_wait_trap + 10, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
* frame #0: 0x0000000112f6551a libsystem_kernel.dylib`semaphore_wait_trap + 10
frame #1: 0x0000000112bde7b4 libdispatch.dylib`_dispatch_thread_semaphore_wait + 16
frame #2: 0x0000000112bdc3a2 libdispatch.dylib`_dispatch_barrier_sync_f_slow + 321
frame #3: 0x000000010fb08f92 CoreData`_perform + 114
frame #4: 0x000000010fb0b7b8 CoreData`-[NSManagedObjectContext(_NestedContextSupport) managedObjectContextDidUnregisterObjectsWithIDs:] + 72
frame #5: 0x000000010fae0d2a CoreData`-[_PFManagedObjectReferenceQueue _processReferenceQueue:] + 1706
frame #6: 0x000000010fb0ff33 CoreData`-[NSManagedObjectContext(_NSInternalNotificationHandling) _processChangedStoreConfigurationNotification:] + 2771
frame #7: 0x000000010fb71ee4 CoreData`__95-[NSManagedObjectContext(_NSInternalNotificationHandling) _sendOrEnqueueNotification:selector:]_block_invoke + 84
frame #8: 0x000000010fb08c7b CoreData`developerSubmittedBlockToNSManagedObjectContextPerform + 107
frame #9: 0x000000010fb08bc4 CoreData`-[NSManagedObjectContext performBlockAndWait:] + 132
frame #10: 0x000000010fb0f2f7 CoreData`-[NSManagedObjectContext(_NSInternalNotificationHandling) _sendOrEnqueueNotification:selector:] + 231
frame #11: 0x000000011289dd9c CoreFoundation`__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
frame #12: 0x000000011280051d CoreFoundation`_CFXNotificationPost + 2381
frame #13: 0x000000011027e7fa Foundation`-[NSNotificationCenter postNotificationName:object:userInfo:] + 68
frame #14: 0x000000010fac06bc CoreData`-[NSPersistentStoreCoordinator(_NSInternalMethods) _postStoresChangedNotificationsForStores:changeKey:options:] + 220
frame #15: 0x000000010fb0e5d6 CoreData`-[NSPersistentStoreCoordinator removePersistentStore:error:] + 278
frame #16: 0x000000010e4bc430 Wingtip`__29-[TTPersistenceManager reset]_block_invoke(.block_descriptor=<unavailable>) + 528 at TTPersistenceManager.m:67
frame #17: 0x000000010fb08c7b CoreData`developerSubmittedBlockToNSManagedObjectContextPerform + 107
frame #18: 0x000000010fb08bc4 CoreData`-[NSManagedObjectContext performBlockAndWait:] + 132
frame #19: 0x000000010e4bc17d Wingtip`-[TTPersistenceManager reset](self=0x00007ff441604e50, _cmd=0x0000000118cfb867) + 237 at TTPersistenceManager.m:59
frame #20: 0x000000010e1dee19 Wingtip`-[WTGatekeeper resetPersistenceManager](self=0x00007ff44195a520, _cmd=0x000000010e8c079e) + 121 at WTGatekeeper.m:118
frame #21: 0x000000011283df1c CoreFoundation`__invoking___ + 140
frame #22: 0x000000011283ddc4 CoreFoundation`-[NSInvocation invoke] + 308
frame #23: 0x000000010e48d141 Wingtip`-[TTStateMachine callTargetsWithEvent:state:](self=0x00007ff44177d2f0, _cmd=0x000000010e8e89a5, event=TTStateMachineEventDidEnter, state=0x000000010ea12f28) + 977 at TTStateMachine.m:277
frame #24: 0x000000010e48aa59 Wingtip`-[TTStateMachine transitionToState:error:](self=0x00007ff44177d2f0, _cmd=0x000000010e8c0919, state=0x000000010ea12f28, error=0x00007fff51a2d168) + 521 at TTStateMachine.m:105
frame #25: 0x000000010e1e0a66 Wingtip`-[WTAuthenticationStateMachine transitionToState:error:](self=0x00007ff44177d2f0, _cmd=0x000000010e8c0919, state=0x000000010ea12f28, error=0x00007fff51a2d168) + 454 at WTAuthenticationStateMachine.m:91
frame #26: 0x000000010e48ac08 Wingtip`-[TTStateMachine transitionToState:](self=0x00007ff44177d2f0, _cmd=0x000000010e8c0746, state=0x000000010ea12f28) + 168 at TTStateMachine.m:119
frame #27: 0x000000010e1e3118 Wingtip`__33-[WTAuthenticateBehavior signOut]_block_invoke(.block_descriptor=<unavailable>, alertView=0x00007ff448075eb0, buttonIndex=1) + 440 at WTAuthenticateBehavior.m:134
frame #28: 0x000000010e4513ac Wingtip`-[MTBlockAlertView alertView:didDismissWithButtonIndex:](self=0x00007ff448075eb0, _cmd=0x00000001116a1fae, alertView=0x00007ff448075eb0, buttonIndex=1) + 108 at MTBlockAlertView.m:77
frame #29: 0x0000000111436451 UIKit`-[UIAlertView modalItem:didDismissWithButtonIndex:] + 115
frame #30: 0x00000001114eea63 UIKit`__87-[_UIModalItemsCoordinator _dismissItem:withTappedButtonIndex:animated:notifyDelegate:]_block_invoke237 + 120
frame #31: 0x0000000111139d32 UIKit`__105-[_UIModalItemsPresentingViewController _dismissItem:andPresentItem:animated:completion:keepDimmingView:]_block_invoke256 + 651
frame #32: 0x00000001110206e8 UIKit`-[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 299
frame #33: 0x000000011100c68e UIKit`-[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 235
frame #34: 0x000000011100c941 UIKit`-[UIViewAnimationState animationDidStop:finished:] + 78
frame #35: 0x0000000110c62134 QuartzCore`CA::Layer::run_animation_callbacks(void*) + 310
frame #36: 0x0000000112bed72d libdispatch.dylib`_dispatch_client_callout + 8
frame #37: 0x0000000112bdd3fc libdispatch.dylib`_dispatch_main_queue_callback_4CF + 354
frame #38: 0x00000001128a6289 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
frame #39: 0x00000001127f3854 CoreFoundation`__CFRunLoopRun + 1764
frame #40: 0x00000001127f2d83 CoreFoundation`CFRunLoopRunSpecific + 467
frame #41: 0x0000000113a0df04 GraphicsServices`GSEventRunModal + 161
frame #42: 0x0000000110fc7e33 UIKit`UIApplicationMain + 1010
frame #43: 0x000000010e1d1e02 Wingtip`main(argc=3, argv=0x00007fff51a2e4a0) + 370 at main.m:19
frame #44: 0x0000000112e3d5c9 libdyld.dylib`start + 1
我最好的猜测是,应用程序中的某些东西仍然存在对托管对象的强引用,现在由于永久对象ID,它还保留了持久性存储协调器。
如果是这种情况,我认为这是一个常见问题。不幸的是,我无法在www。
上找到任何类似内容非常感谢任何帮助!