FetchRequest不返回通过RestKit 0.2

时间:2016-03-30 22:28:10

标签: core-data restkit

我遇到以下问题:我使用RestKit从REST Api获取对象。对象映射有效,我可以从RK调试器输出中看到。但是,当我之后执行获取请求时,结果为空。我在谈论NSManagedObjects。我有以下设置。

1:Restkit和Coredata堆栈初始化:

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]
     ];

描述符看起来像(例子):

[objectManager addResponseDescriptor:
     [RKResponseDescriptor responseDescriptorWithMapping:[shoutMapping inverseMapping]
                                                  method:RKRequestMethodPOST
                                             pathPattern: @"login/IOS"
                                                 keyPath:@"response.incomingshoutapplications"
                                             statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)
      ]
     ];

呼喊映射如下:

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]调用; (见第一段代码)

1 个答案:

答案 0 :(得分:0)

问题可能是你正在使用inverseMapping。它主要用于请求映射,因为它是一个创建NSMutableDictionary实例的映射,它不是你想要的。

所以,我期望发生的是响应被成功映射,但是对于普通对象而不是被管理对象,然后你将结果丢弃。

只需更改为

... responseDescriptorWithMapping:shoutMapping ...