RKMappingresult使用restkit在64位iphone中返回不同的结果

时间:2014-05-23 08:07:14

标签: ios iphone objective-c restkit rkobjectmapping

我对Restkit有一个奇怪的问题。我正在做以下事情:

-(void)doLogin:(NSString *)email andPassword:(NSString *)password OnCompletion:(myCompletion) compblock{
    Mapper *mapper = [Mapper new];
    RKManagedObjectStore *store = [[OffitelDataModel sharedDataModel] objectStore];
    NSLog(@"store is %@",store);
    NSManagedObjectContext *context = store.mainQueueManagedObjectContext;
    RKObjectManager *objectManager = [mapper mapLogin];
    NSString *deviceToken = [[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"];
    NSString *urlString = [NSString stringWithFormat:@"company-user/login/%@?email=%@&pwd=%@&ios_id=%@",apikey,email,password,deviceToken];
    NSURLRequest *request = [objectManager requestWithObject:nil method:RKRequestMethodGET path:urlString parameters:nil];
    RKManagedObjectRequestOperation *operation = [objectManager managedObjectRequestOperationWithRequest:request managedObjectContext:context success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        NSError *error = nil;
        BOOL success = [context  save:&error];
        if (!success) RKLogWarning(@"Failed saving managed object context: %@", error);
        Data *data2 = [mappingResult.array objectAtIndex:0];
        NSLog(@"MAPPING RESULT 0 = %@",[mappingResult.array objectAtIndex:0]);
        NSLog(@"data status is %@",data2.webstatus);
        int value = [data2.webstatus intValue];
        if (value == 200){
            Person  *personObject = [mappingResult.array objectAtIndex:2];
            NSString *name = [NSString stringWithFormat:@"%@ %@",personObject.cu_first_name,personObject.cu_last_name];
            NSDictionary *dictUser = [[NSDictionary alloc]initWithObjectsAndKeys:personObject.cu_id,@"personId",personObject.company.c_id,@"companyId",name,@"personName",personObject.cu_status_id,@"statusId", nil];
            [[NSUserDefaults standardUserDefaults]setObject:dictUser forKey:@"user"];
            [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithBool:YES] forKey:@"loggedIn"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            compblock(YES);
        }else{
            //show validation
            NSLog(@"ERROR");
        }
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
         NSLog(@"ERROR");
    }];
    [objectManager enqueueObjectRequestOperation:operation];
}

这是我的映射

-(RKObjectManager *)mapLogin{

    RKObjectMapping* dataMapping = [RKObjectMapping mappingForClass:[Data class]];
    [dataMapping addAttributeMappingsFromDictionary:@{
                                                      @"status": @"webstatus",
                                                      @"message": @"message",
                                                      @"text": @"text"
                                                      }];
    RKEntityMapping* personMapping = [RKEntityMapping mappingForEntityForName:@"Person" inManagedObjectStore:managedObjectStore];
    personMapping.identificationAttributes = @[@"cu_id"] ;
    [personMapping addAttributeMappingsFromDictionary:@{
                                                        @"cu_id":              @"cu_id",
                                                        @"cu_status_id":       @"cu_status_id",
                                                        @"cu_company_id":      @"cu_company_id",
                                                        @"cu_function_id":     @"cu_function_id",
                                                        @"cu_department_id":   @"cu_department_id",
                                                        @"cu_email":           @"cu_email",
                                                        @"cu_first_name":      @"cu_first_name",
                                                        @"cu_last_name":       @"cu_last_name",
                                                        @"cu_phone_intern":    @"cu_phone_intern",
                                                        @"cu_mobile_phone":    @"cu_mobile_phone",
                                                        @"cu_street":          @"cu_street",
                                                        @"cu_number":          @"cu_number",
                                                        @"cu_bus":             @"cu_bus",
                                                        @"cu_postalcode":      @"cu_postalcode",
                                                        @"cu_location":        @"cu_location",
                                                        @"cu_country":         @"cu_country",
                                                        @"cu_birthdate":       @"cu_birthdate",
                                                        @"cu_picture":         @"cu_picture",
                                                        @"cu_comment":         @"cu_comment",
                                                        @"cu_ison_reminder_email": @"cu_ison_reminder_email",
                                                        @"cu_ison_reminder_app":   @"cu_ison_reminder_app",
                                                        @"cu_ison_reminder_web":   @"cu_ison_reminder_web",
                                                        @"cu_first_use":       @"cu_first_use"
                                                        }];
    RKEntityMapping* functionMapping = [RKEntityMapping mappingForEntityForName:@"Function" inManagedObjectStore:managedObjectStore];
    functionMapping.identificationAttributes = @[@"cf_id"] ;
    [functionMapping addAttributeMappingsFromDictionary:@{
                                                          @"cf_id": @"cf_id",
                                                          @"cf_name":@"cf_name"
                                                          }];
    RKEntityMapping* departmentMapping = [RKEntityMapping mappingForEntityForName:@"Department" inManagedObjectStore:managedObjectStore];
    departmentMapping.identificationAttributes = @[@"cd_id"] ;
    [departmentMapping addAttributeMappingsFromDictionary:@{
                                                            @"cd_id": @"cd_id",
                                                            @"cd_name":@"cd_name"
                                                            }];
    RKEntityMapping* companyMapping = [RKEntityMapping mappingForEntityForName:@"Company" inManagedObjectStore:managedObjectStore];
    companyMapping.identificationAttributes = @[@"c_id"] ;
    [companyMapping addAttributeMappingsFromDictionary:@{
                                                         @"c_id": @"c_id",
                                                         @"c_name":@"c_name",
                                                         @"c_phone":@"c_phone",
                                                         @"c_fax":@"c_fax",
                                                         @"c_website":@"c_website"
                                                         }];

    RKEntityMapping* statusMapping = [RKEntityMapping mappingForEntityForName:@"Status" inManagedObjectStore:managedObjectStore];
    statusMapping.identificationAttributes = @[@"cs_id"] ;
    [statusMapping addAttributeMappingsFromDictionary:@{
                                                        @"cs_id": @"cs_id",
                                                        @"cs_company_id":@"cs_company_id",
                                                        @"cs_name":@"cs_name",
                                                        @"cs_default":@"cs_default",
                                                        @"cs_image":@"cs_image"
                                                        }];
    RKRelationshipMapping* relationFunctionMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"function"toKeyPath:@"function"withMapping:functionMapping];
    RKRelationshipMapping* relationDepartmentMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"department"toKeyPath:@"department"withMapping:departmentMapping];
    RKRelationshipMapping* relationCompanyMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"company"toKeyPath:@"company"withMapping:companyMapping];
    RKRelationshipMapping* relationStatusMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"statuses"toKeyPath:@"status"withMapping:statusMapping];

    [personMapping addPropertyMapping:relationFunctionMapping];
    [personMapping addPropertyMapping:relationDepartmentMapping];
    [personMapping addPropertyMapping:relationCompanyMapping];
    [companyMapping addPropertyMapping:relationStatusMapping];
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:dataMapping
                                                                                       pathPattern:nil
                                                                                           keyPath:@"data"                                                                                       statusCodes:[NSIndexSet indexSetWithIndex:200]];
    RKResponseDescriptor *responseDescriptor2 = [RKResponseDescriptor responseDescriptorWithMapping:personMapping
                                                                                        pathPattern:nil
                                                                                            keyPath:@"data.user"
                                                                                        statusCodes:[NSIndexSet indexSetWithIndex:200]];
    RKResponseDescriptor *responseDescriptor3 = [RKResponseDescriptor responseDescriptorWithMapping:companyMapping
                                                                                        pathPattern:nil
                                                                                            keyPath:@"data.user.company"
                                                                                        statusCodes:[NSIndexSet indexSetWithIndex:200]];
    NSArray *arrResponsDescriptor = [[NSArray alloc]initWithObjects:responseDescriptor,responseDescriptor2,responseDescriptor3, nil];

    [objectManager addResponseDescriptorsFromArray:arrResponsDescriptor];
    return objectManager;
}

奇怪的是,在大多数手机上它都能正常工作,但仅在64-bit devices出错。

当我看这个NSLog时

NSLog(@"MAPPING RESULT 0 = %@",[mappingResult.array objectAtIndex:0]);

我在not-64-bit devices中看到它返回Data class个对象。那就好了。但是在64-bit devices中,它会返回Company-object并且那不行!

有人可以帮我吗?

亲切的问候

1 个答案:

答案 0 :(得分:1)

您有多个响应描述符,每个描述符都有一个nil路径模式,因此它们将始终应用于任何响应。 RestKit不保证调用它们的顺序。它也不保证mappingResult.array

内容的顺序

您应该使用响应描述符上的密钥路径来访问每个描述符的结果。 mappingResult还为您提供了一个字典(而不是数组),您可以使用响应描述符密钥路径来访问相关结果。用它将Data结果与Company结果分开。