[英]Objects are not saved into SQL Lite even though it says it was saved [RestKit ManagedObjectContext]
所以我遇到了一個非常令人沮喪的問題......我正在使用Restkit從我的Django服務器獲取對象,並且映射已成功完成。 那樣就好! 現在,我試圖從我的SQLLite數據庫中獲取該對象並更改該對象並將其保存回來。 例如:
_managedObjectContext = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"User"];
fetchRequest.predicate = [NSPredicate predicateWithFormat: @"identifier == 3"];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"identifier" ascending:NO];
fetchRequest.sortDescriptors = @[descriptor];
NSError *error = nil;
NSFetchedResultsController *fetchedResultsController2 = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:_managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
BOOL fetchSuccessful = [fetchedResultsController2 performFetch:&error];
if (! fetchSuccessful) {
NSLog(@"%@", error.description);
}
PokaUser* user = [[fetchedResultsController2 fetchedObjects]objectAtIndex:0];
user.firstName = @"NewFirstName";
BOOL hasSaved = [user.managedObjectContext save:&error];
NSLog(@"INFO:{%s} CoreData has Saved: %@ (%d)\nerror:%@ | %@ | %@", __FUNCTION__, (hasSaved) ? @"YES" : @"NO", hasSaved, error, [error userInfo],[error localizedDescription]);
我得到以下日志:
CoreData已保存:YES(1)錯誤:(null)| (null)| (空值)
現在,如果我關閉我的應用程序並重新打開它,(或者甚至自己檢查Sqllite.db),則不會保存更改。 我一直得到第一個名字。
這是我創建ManagedObjectContext的方法:
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Maindb.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"Maindb" ofType:@"sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
有任何想法嗎?!
謝謝!
有必要調用托管ObjectStore的savePersistentStore方法:[managedObjectStore.mainQueueManagedObjectContext save:&error]; [managedObjectStore.mainQueueManagedObjectContext saveToPersistentStore:&error];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.