Objective c 将多个文件上载到一行parse.com
因此,我使用以下代码将多个文件上载到类的一行Objective c 将多个文件上载到一行parse.com,objective-c,parse-platform,Objective C,Parse Platform,因此,我使用以下代码将多个文件上载到类的一行 for (NSString* currentString in directoryContents){ NSLog(@"%@",currentString); NSString *temp2 = [temp stringByAppendingPathComponent:currentString]; PFFile *file = [PFFile fileWithName:currentString contentsAtPa
for (NSString* currentString in directoryContents){
NSLog(@"%@",currentString);
NSString *temp2 = [temp stringByAppendingPathComponent:currentString];
PFFile *file = [PFFile fileWithName:currentString contentsAtPath:temp2];
[file saveInBackground];
PFObject *obj = [PFObject objectWithClassName:@"DreamBits"];
if ([currentString isEqualToString:@"index.html"]) {
[obj setObject:file forKey:@"index"];
}
else {
count += 1;
NSString *filen = [NSString stringWithFormat:@"file%i",count];
NSLog(@"%@",filen);
[obj setObject:file forKey:filen];
}
[obj saveInBackground];
}
问题是,我会因为某种原因而感到沮丧。你知道我该怎么更正吗?我对你的代码做了一点修改。我不运行此代码,但希望它能帮助您
PFObject *obj = [PFObject objectWithClassName:@"DreamBits"];
for (NSString* currentString in directoryContents){
NSLog(@"%@",currentString);
NSString *temp2 = [temp stringByAppendingPathComponent:currentString];
PFFile *file = [PFFile fileWithName:currentString contentsAtPath:temp2];
if ([currentString isEqualToString:@"index.html"]) {
[obj setObject:file forKey:@"index"];
}
else {
count += 1;
NSString *filen = [NSString stringWithFormat:@"file%i",count];
NSLog(@"%@",filen);
[obj setObject:file forKey:filen];
}
}
[obj saveInBackground];
在循环外部创建PFObject。将所有PFFile对象设置为循环内部的PFObject
循环后,保存PFObject。最好使用以下方法:
[obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
//Check whether the upload is succeeded or not
}];
我使用此方法在Parse Server表中的一行中上载配置文件和拇指图像。使用swift 4和Parse SDK 1.17.1。希望这项技术能有所帮助
func uploadImage(profileImage: UIImage, profileImageName: String, thumbImage: UIImage, thumbImageName: String) {
if let profileImagefile = PFFile.init(name: profileImageName, data: profileImage.jpegData(compressionQuality: 1)!) {
let fileObject = PFObject(className:"ProfileImage")
fileObject.setValue(profileImagefile, forKey: "profile_image")
fileObject.saveInBackground { (success, error) in
if error == nil {
print("thumb image path: \(profileImagefile.url ?? "")")
if let thumbImage = PFFile.init(name: thumbImageName, data: thumbImage.jpegData(compressionQuality: 0.5)!) {
fileObject.setValue(thumbImage, forKey: "thumb_image")
fileObject.saveInBackground(block: { (result, fileError) in
if fileError == nil {
print("thumb image path: \(thumbImage.url ?? "")")
}else {
print("error on thumb upload: \(result)")
}
})
}
}else {
print("error on file upload: \(error.debugDescription)")
}
}
}
}
您还需要等待文件保存完成后再将其附加到对象,这在Objective-C中有点困难,没有承诺。@TimothyWalters是的,我同意。文件必须在附加到PFObject之前上载。而且很难跟踪多个文件。由于这个原因,我不会单独上传文件。在上面的代码中,PFFile将作为PFObject保存操作的一部分上传。