3

我正在使用 RestKit 0.20 将 JSON 请求提取到核心数据中。出于某种原因,我必须删除实体(即 myEntity)中的所有对象。我正在使用以下代码这样做:

NSManagedObjectContext *moc = self.objectManager.managedObjectStore.mainQueueManagedObjectContext;

[moc saveToPersistentStore:nil];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:@"myEntity"
                                  inManagedObjectContext:moc];
fetchRequest.includesPropertyValues = NO;
NSArray *entries = [moc executeFetchRequest:fetchRequest error:nil];

for (myEntity *entity in entries) {
    [moc deleteObject:entity];
}

[moc saveToPersistentStore:nil];

之后,下一个 RKObjectRequestOperation 输出是“(200 OK / 0 个对象)”。但实际上,JSON 文档中大约有 700 个对象。如果我[[NSURLCache sharedURLCache] removeAllCachedResponses];最后打电话,我会得到所有 700 个对象,但我有 2 个 RestKit 错误(133000):E restkit.core_data.cache:RKEntityByAttributeCache.m:227 无法检索托管对象...谁能帮我怎么做这是正确的方法吗?

编辑:这就是我创建核心数据堆栈的方式:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"myDataModelName" withExtension:@"momd"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

[self.managedObjectStore createPersistentStoreCoordinator];
[self.managedObjectStore
 addSQLitePersistentStoreAtPath:self.pathToDatabase
 fromSeedDatabaseAtPath:nil
 withConfiguration:nil
 options:@{ NSInferMappingModelAutomaticallyOption: @YES,
            NSMigratePersistentStoresAutomaticallyOption: @YES }
 error:nil];

[self.managedObjectStore createManagedObjectContexts];

NSManagedObjectContext *moc = self.managedObjectStore.persistentStoreManagedObjectContext;
self.managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:moc];

managedObjectStore.persistentStoreManagedObjectContext和有区别managedObjectStore.mainQueueManagedObjectContext吗?

4

1 回答 1

1

Preferably you should specify a fetch request block on the object manager and allow RestKit to handle the deletion of the objects. The fetch request block allows RestKit to find all of the objects that need to be deleted during the mapping process while maintaining any internal caching and maximising data reuse. Check the docs here (particularly section "Fetch Request Blocks and Deleting Orphaned Objects").


Yes, there is a difference between managedObjectStore.persistentStoreManagedObjectContext and managedObjectStore.mainQueueManagedObjectContext.

The header Cache-Control: public, max-age=180 that the server returns tells the URL caching system not to request new results for any request that was made less than 3 minutes ago. So, during that time, RestKit will not actually send a request and will get objects from the model instead.

Ideally you should change the returned headers. But, effectively, that should really be done automatically because your request should be different if it's for a different user to the one who was previously logged in.

于 2013-11-05T23:15:47.923 回答