Core data FetchRequest不返回通过RestKit 0.2获取的NSManagedObject
我有以下问题:我使用RestKit从RESTAPI获取对象。对象映射可以工作,我可以从RK调试器输出中看到这一点。但是,当我随后执行一个获取请求时,结果是空的。我说的是NSManagedObjects。我有以下设置 1:Restkit和Coredata堆栈初始化:Core data FetchRequest不返回通过RestKit 0.2获取的NSManagedObject,core-data,restkit,Core Data,Restkit,我有以下问题:我使用RestKit从RESTAPI获取对象。对象映射可以工作,我可以从RK调试器输出中看到这一点。但是,当我随后执行一个获取请求时,结果是空的。我说的是NSManagedObjects。我有以下设置 1:Restkit和Coredata堆栈初始化: NSError *error; NSURL *baseURL = [NSURL URLWithString:@"https://mfihost.us/gocoffee/api/V1/"]; RKObjectManager *objec
NSError *error;
NSURL *baseURL = [NSURL URLWithString:@"https://mfihost.us/gocoffee/api/V1/"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
[objectManager.HTTPClient setDefaultHeader:@"Token" value:[NSString stringWithFormat:@"%@",[FBSDKAccessToken currentAccessToken].tokenString]];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
//[RKObjectManager setSharedManager:objectManager];
[FetchRequests registerFetchRequests:objectManager];
[Descriptors registerDescriptors:objectManager];
[managedObjectStore createPersistentStoreCoordinator];
NSPersistentStore *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error];
NSAssert(persistentStore, @"Failed to add inmemory store with error: %@", error);
[managedObjectStore createManagedObjectContexts];
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
2:调用以从服务器获取对象,然后执行提取请求:
[[RKObjectManager sharedManager]
postObject:nil path:@"/gocoffee/api/V1/login/IOS"
parameters:nil
success: ^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"Objects have been saved in core data.");
NSManagedObjectContext *managedObjCtx = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
// Shout* sh=[managedObjCtx insertNewObjectForEntityForName:@"Shout"];
// sh.inviterUserId=@"22223";
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Shout" inManagedObjectContext:managedObjCtx];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *result = [managedObjCtx executeFetchRequest:fetchRequest error:&error];
if (error) {
NSLog(@"Unable to execute fetch request.");
NSLog(@"%@, %@", error, error.localizedDescription);
} else {
NSLog(@"%@", result);
}
completionBlock();
}
failure: ^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Load failed with error: %@", error);
}];
获取结果为空,尽管服务器返回对象,并且这些对象通过使用RKEntityMappings和相应的响应描述符正确映射。令人困惑的是,如果我取消注释这两行//Shout*。。。。(即,手动将托管项目插入上下文),然后通过获取请求获取此对象。因此,获取请求应该可以正常工作。
我正在寻找问题可能是什么。可能是我打错电话了吗?顺便说一下:核心数据多线程调试已启用,并且没有显示任何错误,即没有“AllThatIsLeftToUsIsHonor”错误
上述示例中的相应路线为:
[objectManager.router.routeSet
addRoute:[RKRoute routeWithName:@"loginAndOrSignup"
pathPattern:@"login/IOS"
method:RKRequestMethodPOST]
];
描述符如下所示(示例):
具体情况如下:
RKEntityMapping *shoutMapping = [RKEntityMapping mappingForEntityForName:@"Shout" inManagedObjectStore:managedObjectStore];
shoutMapping.identificationAttributes = @[ @"id" ];
[shoutMapping addAttributeMappingsFromDictionary:@{
@"id" : @"id",
@"inviterUserId" : @"inviterUserId",
@"eventType" : @"eventType",
@"eventTime" : @"eventTime",
@"locationLat" : @"locationLat",
@"locationLng" : @"locationLng",
@"radius" : @"radius",
@"targetGender" : @"targetGender",
@"state" : @"state",
@"buddyOnly" : @"buddyOnly",
@"timeCreated" : @"timeCreated"
}
];
“manager”是上面的一个,managedObjectStore是manager.managedObjectStore
所有映射和描述符都在另一个方法中设置,该方法由[descriptors registerDescriptors:objectManager]调用;(请参阅第一段代码)问题可能是您正在使用
反向映射。这主要用于请求映射,因为它是一个创建NSMutableDictionary
实例的映射,而这不是您想要的
因此,我期望发生的是,响应被成功映射,但映射到普通对象,而不是托管对象,然后将结果丢弃
换成
... responseDescriptorWithMapping:shoutMapping ...
显示映射代码和一段日志,其中显示了末尾的映射对象对不起,回复太晚了…我将很快尝试一下,并告诉您它是否有效:-)但是已经谢谢了。。。
... responseDescriptorWithMapping:shoutMapping ...