我正在使用 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
吗?