2014-07-21 2 views
1

이미지와 함께 새로운 관리 객체를 웹 데이터베이스에 올리려고하는데, 약간 이상한 버그를 제외한 모든 것이 저에게 잘 돌아갑니다. 관리 객체가 성공적으로 업로드되면 UITableView에 2 개의 행이 추가됩니다. UITableView를 새로 고칠 때 이전에 추가 된 UITableViewCell 중 하나가 업로드 된 이미지로 채워집니다. 앱을 다시 빌드하면 모든 것이 잘 작동합니다. 이전에 추가 된 관리 객체는 중복없이 올바르게 표시됩니다. 누구든지 내 코드를 살펴보고 그 버그의 원인을 알려주고 어떻게 해결할 수 있습니까? 더 많은 코드가 필요한 경우 알려 주시기 바랍니다.Restkit을 사용하여 이미지 업로드하기 - 하나 대신 UITableView에 두 행 추가하기

-(void)postRequest{ 
NSEntityDescription *watchEntityDesc = [NSEntityDescription entityForName:@"Watches" inManagedObjectContext:[[RKObjectManager sharedManager]managedObjectStore].mainQueueManagedObjectContext]; 

Watches *watch = [[Watches alloc]initWithEntity:watchEntityDesc insertIntoManagedObjectContext:[[RKObjectManager sharedManager]managedObjectStore].mainQueueManagedObjectContext]; 
watch.phonewatchno = @"124512"; 
watch.latitude = [NSNumber numberWithDouble:-33.856870]; 
watch.longitude = [NSNumber numberWithDouble:151.215279]; 


NSEntityDescription *wearersEntityDesc = [NSEntityDescription entityForName:@"Wearers" inManagedObjectContext:[[RKObjectManager sharedManager]managedObjectStore].mainQueueManagedObjectContext]; 

Wearers *wearer = [[Wearers alloc]initWithEntity:wearersEntityDesc insertIntoManagedObjectContext:[[RKObjectManager sharedManager]managedObjectStore].mainQueueManagedObjectContext]; 
wearer.name =_nameTextField.text, 
wearer.watches =[NSSet setWithObject:watch]; 

RKEntityMapping *watchesMapping = [RKEntityMapping mappingForEntityForName:@"Watches" inManagedObjectStore:[[EFDateModel sharedDataModel]objectStore]]; 
[watchesMapping addAttributeMappingsFromDictionary:@{ 

                @"id": @"watch_id", 
                @"latitude":@"latitude", 
                @"longitude":@"longitude", 
                @"phonewatchno":@"phonewatchno", 

                } 
]; 

[watchesMapping addConnectionForRelationship:@"wearer" connectedBy:@{ 
                    @"wearer_id":@"wearer_id" 
                    }]; 
[watchesMapping setIdentificationAttributes:@[@"watch_id"]]; 

RKResponseDescriptor *responseDescr = [RKResponseDescriptor responseDescriptorWithMapping:watchesMapping method:RKRequestMethodPOST pathPattern:@"/watches.json" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 


[[RKObjectManager sharedManager]addResponseDescriptor:responseDescr]; 

RKEntityMapping *wearersMapping = [RKEntityMapping mappingForEntityForName:@"Wearers" inManagedObjectStore:[[EFDateModel sharedDataModel] objectStore]]; 

[wearersMapping addAttributeMappingsFromDictionary:@{ 
                @"id":@"wearer_id", 
                @"name":@"name", 

                } 
]; 
wearersMapping.identificationAttributes = @[@"wearer_id"]; 

[wearersMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"watch" toKeyPath:@"watches" withMapping:watchesMapping]]; 


RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:wearersMapping method:RKRequestMethodPOST pathPattern:@"/wearers.json" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 


RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[wearersMapping inverseMapping] objectClass:[Wearers class] rootKeyPath:nil method:RKRequestMethodPOST ]; 

[[RKObjectManager sharedManager]addResponseDescriptor:responseDescriptor]; 
[[RKObjectManager sharedManager]addRequestDescriptor:requestDescriptor]; 

[[RKObjectManager sharedManager]setRequestSerializationMIMEType:RKMIMETypeJSON]; 
[[RKObjectManager sharedManager]setAcceptHeaderWithMIMEType:@"application/json"]; 




UIImage *image =[UIImage imageWithCGImage:_wearerImage.CGImage scale:0.4 orientation:UIImageOrientationUp]; 
NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestWithObject:wearer method:RKRequestMethodPOST path:@"/wearers.json" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
    [formData appendPartWithFileData:UIImagePNGRepresentation(image) 
           name:@"wearer_photo" 
          fileName:@"photo.png" 
          mimeType:@"image/png"]; 
}]; 

NSManagedObjectContext *moc =[[[RKObjectManager sharedManager]managedObjectStore]mainQueueManagedObjectContext]; 
RKManagedObjectRequestOperation *managedObjectOperation = [[RKObjectManager sharedManager]managedObjectRequestOperationWithRequest:request managedObjectContext:moc success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
    NSLog(@"Mapping result = %@",mappingResult.array); 
    [[RKObjectManager sharedManager]removeResponseDescriptor:responseDescriptor]; 
    [[RKObjectManager sharedManager]removeRequestDescriptor:requestDescriptor]; 
    [[NSNotificationCenter defaultCenter] 
    postNotificationName:@"ReloadTable" 
    object:self]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} failure:^(RKObjectRequestOperation *operation, NSError *error) { 
    NSLog(@"Error : \n %@",error.description); 
}]; 
[[RKObjectManager sharedManager] enqueueObjectRequestOperation:managedObjectOperation]; 
} 

건배

답변

0

때마다 당신은 당신이 두 번 alloc]initWithEntity:... insertIntoManagedObjectContext:를 호출이 메서드를 호출하고 두 개의 새로운 엔티티 인스턴스를 만들 수 있습니다. 당신은 아마도 엔티티 인스턴스를 사용하고 새로운 인스턴스를 생성하지 않는이 메소드에 엔티티 인스턴스를 전달해야합니다.

또한 새 디스크립터를 계속 만들고 추가하거나 제거하는 것은 비효율적입니다. 동시에 2 개의 요청을하려고하면 오류가 발생합니다. 처음 사용하기 전에 개체 관리자를 한 번 구성한 다음 다시 구성하지 않고 반복적으로 사용하십시오.

관련 문제