我正在尝试将带有图像的新托管对象发布到Web数据库,如下所示,除了小怪异的bug之外,一切正常。成功上载托管对象后,将向UITableView添加2行而不是1行。当我刷新UITableView时,之前添加的UITableViewCell之一正在填充上传的图像。当我重建应用程序时,一切正常。以前添加的托管对象正确显示,无需重复。有没有人可以看看我的代码并告诉我该错误的原因是什么,我该如何解决?如果您需要更多代码,请与我们联系。
-(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 :(得分:0)
每次调用此方法时,您都会调用alloc]initWithEntity:... insertIntoManagedObjectContext:
两次并创建两个新的实体实例。您可能应该将实体实例传递给它使用的此方法,而不是创建任何新实例。
此外,继续创建新描述符并添加和删除它们是低效的。如果您尝试同时发出2个请求,则会出现错误。首次使用之前配置对象管理器一次,然后重复使用它而不重新配置。