解析关系中的多个图像

时间:2014-08-11 12:07:35

标签: ios objective-c parse-platform

我试图在多个图像与对象之间建立关系。我一直听到以下错误:

错误代码

All objects in a relation must have object ids

我做错了什么?我试图按照文档进行调整并将其调整为我的示例。

关系代码。

    homeObject = [PFObject objectWithClassName:@"Homes"];
    homeObject[@"userId"] = [PFUser currentUser].objectId;       
    for (int i = 0; i < [self.assets count]; i++) {                        
        PFObject *object = [PFObject objectWithClassName:@"homeImages"];
        ALAsset *asset = [self.assets objectAtIndex:i];
        ALAssetRepresentation *representation = asset.defaultRepresentation;            
        UIImage *fullResolutionImage =
        [UIImage imageWithCGImage:representation.fullResolutionImage
                            scale:1.0f
                      orientation:(UIImageOrientation)representation.orientation];            
        NSData *imgData= UIImageJPEGRepresentation(fullResolutionImage,0.0);
        PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:imgData];
        object[@"Image"] = imageFile;
        [object saveInBackground];
        PFRelation *relation = [homeObject relationforKey:@"imageId"];
        [relation addObject:object];                                   
    }
    [homeObject saveInBackground];

1 个答案:

答案 0 :(得分:0)

您需要保存文件并等待它完成保存(在后台),然后您可以安全地将其附加到homeImages对象。接下来,您需要保存该对象并等待它完成保存(在后台),然后您最终可以将其添加到关系并保存父对象。

由于没有内置的promises支持(你可能想看一些第三方的支持),你将不得不使用嵌套。

考虑使用PFFile创建saveAllInBackground:block:个对象数组。在块中创建homeImages个对象并将它们放入一个数组中,再次调用save-all。最后在完成块中将它们全部添加到关系中,并在saveInBackground上调用homeObject