![](/img/trans.png)
[英]Using NSPrivateQueueConcurrencyType child context with NSPrivateQueueConcurrencyType parent context
[英]Child ManagedObjectContext with NSPrivateQueueConcurrencyType after resetting parent PersistenceStoreCoordinator
當用戶登錄到我的應用程序時,我會在后台線程中從服務器下載一些數據。 我有一個單例,將其managedObjectContext設置為主要managedobjectcontext的子代。
-(NSManagedObjectContext*) context{
if(!_context){
NSManagedObjectContext *child = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[child setParentContext:[MyAppDelegate delegate].managedObjectContext];
_context = child;
}
return _context;
}
當用戶注銷時,我刪除鎖定主要托管對象上下文的PersistenceStoreCoordinator sqllite文件
-(void)onLogout{
NSError *error = nil;
if ([_persistentStoreCoordinator persistentStores] == nil)
return;
[self.managedObjectContext reset];
[self.managedObjectContext lock];
NSPersistentStore *store = [[self.persistentStoreCoordinator persistentStores] lastObject];
if (![self.persistentStoreCoordinator removePersistentStore:store error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
// Delete file
if ([[NSFileManager defaultManager] fileExistsAtPath:store.URL.path]) {
if (![[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
_persistentStoreCoordinator = nil;
_persistentStoreCoordinator = [self persistentStoreCoordinator];
[self.managedObjectContext unlock];
self.agendaLoader = nil;
_agendaLoader = self.agendaLoader;
}
如果我在同一模擬器會話中注銷/登錄(不停止應用程序),則單例中的save方法將崩潰,並顯示以下錯誤:
This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'
但是,如果我注銷/停止應用程序/重新啟動應用程序/登錄,它沒有任何問題。
我嘗試在注銷時重設子托管對象上下文,但沒有任何結果。
是否有任何最佳實踐來重置所有托管對象上下文,刪除storecoordinator文件以及“重新引導”所有核心數據堆棧?
謝謝你的幫助。
我認為通知是您的最佳選擇。 持有對托管對象上下文以及任何托管對象的引用的每個視圖控制器或對象都必須遵守自定義通知以進行重置。 由於所有這些都將失效。
我很好奇,您需要刪除永久存儲文件的原因是什么? 是出於隱私或安全原因? 是否只是數據的臨時性質?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.