上传图像与Restkit -添加两行到UITableView而不是一个



我正试图将新的管理对象与图像发布到web数据库,如下所示,除了奇怪的小bug之外,一切都很好。当管理对象成功上传时,将向UITableView添加2行而不是1行。当我刷新UITableView时,先前添加的UITableViewCell正在填充上传的图像。当我重新构建应用程序时,一切都很好。正确显示之前添加的管理对象,没有重复。有没有人可以看看我的代码,告诉我这个bug的原因是什么,我该如何修复它?如果你需要更多的代码,请告诉我。

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

欢呼

每次调用此方法时,您都会调用alloc]initWithEntity:... insertIntoManagedObjectContext:两次并创建两个新的实体实例。你可能应该传递一个实体实例给它使用的这个方法,而不是创建任何新的实例。

同样,不断创建新的描述符并添加和删除它们是低效的。如果你试图同时发出两个请求,你会得到一个错误。在第一次使用对象管理器之前配置一次,然后重复使用它而不重新配置。

相关内容

最新更新