Загрузка нескольких файлов в одну строку parse.com
Поэтому я использую следующий код для загрузки нескольких файлов в одну строку класса.
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];
}
Проблема в том, что я получаю файлы в разных строках по какой-то причине. Есть идеи, как я могу это исправить?
1 ответ
Решение
Я немного изменил твой код. Я не запускаю этот код, но надеюсь, что он вам поможет.
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)")
}
}
}
}