RKMappingresult returning different result in 64-bit iphones with restkit

242 views Asked by At

I have a strange problem with Restkit. I'm doing the following:

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

En this is my mapping

-(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;
}

The strange this is that on most phones it all works correct but only on 64-bit devices are going wrong.

When I look at this NSLog

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

I see that in the not-64-bit devices it returns the Data class object. And thats oké. But in the 64-bit devices it returns the Company-object and that's not ok !

Can somebody help me with this ?

Kind regards

1

There are 1 answers

2
Wain On

You have multiple response descriptors each with a nil path pattern, so they will always be applied to any response. RestKit does not guarantee the order in which they will be called. It also doesn't guarantee the order of the contents of mappingResult.array.

You should be using your key paths on the response descriptors to access the results of each descriptor. The mappingResult also offers you a dictionary (instead of array) where you can use the response descriptor key path to access the associated results. Use that to separate the Data results from the Company results.