使用 RestKit 0.20 发布 - NSManagedObjectContext 问题
Posted
技术标签:
【中文标题】使用 RestKit 0.20 发布 - NSManagedObjectContext 问题【英文标题】:POST with RestKit 0.20 - NSManagedObjectContext issue 【发布时间】:2013-09-04 09:49:49 【问题描述】:我正在尝试使用 RestKit 发出 POST 请求,但是当我调用 RKObjectManager 的 postObject 方法时,我收到以下错误:
Can only use -performBlockAndWait: on an NSManagedObjectContext that was created with a queue.
RKManagedObjectRequestOperation 类调用方法 performBlockAndWait 时出现此错误。
这是我的代码:
RKObjectMapping *responseMapping = [RKObjectMapping requestMapping];
[responseMapping addAttributeMappingsFromDictionary:@
// ...
];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
NSString *pathPattern = @"myPath";
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodPOST pathPattern:pathPattern keyPath:nil statusCodes:statusCodes];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@
// ...
];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[MyClass class] rootKeyPath:@"" method:RKRequestMethodPOST];
NSURL *baseURL = [NSURL URLWithString:@"http://192.168.1.1:8080"];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:baseURL];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
manager.managedObjectStore = managedObjectStore;
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"mysqliteDatabaseFile"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
[managedObjectStore createManagedObjectContexts];
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
// POST to create
@try
[manager postObject:visitReport path:@"myPath" parameters:nil success:^( RKObjectRequestOperation *operation , RKMappingResult *mappingResult )
NSLog(@"Success");
failure:^( RKObjectRequestOperation *operation , NSError *error )
NSLog(@"Failure - %@",error);
];
@catch (NSException *exception)
NSLog(@"error - %@", exception);
我不明白我的上下文的问题,欢迎任何帮助。 谢谢。
【问题讨论】:
您正在使用postObject
,但您没有任何映射来将响应应用于已发布的对象?发布的对象是一个实体,但您想映射回字典?
正确,我不必映射回字典。但我仍然有最初的错误
【参考方案1】:
好吧,我终于找到问题了:
我尝试发布的对象 visitReport 不是来自 RestKit 预期的 NSManagedObjectContext。
为了解决,我创建了一个新对象:
MyObject *visitReportContext = [NSEntityDescription insertNewObjectForEntityForName:@"tableName"
inManagedObjectContext:objectStore.mainQueueManagedObjectContext];
visitReportContext.Id = visitReport.Id
visitReportContext.title = visitReport.title
// etc..
然后,当我发布我的新对象时,它就可以工作了。但它不是很漂亮......我如何处理我的旧对象 visitReport 而不在这个“RestKit 上下文”中创建一个新对象?
【讨论】:
以上是关于使用 RestKit 0.20 发布 - NSManagedObjectContext 问题的主要内容,如果未能解决你的问题,请参考以下文章