Search code examples
objective-cparse-platform

Uploading multiple files to a single row parse.com


So I'm using the following code to upload multiple files to a single row of a class.

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

The issue is I'm getting the files in different rows for some reason. Any idea how I can correct this?


Solution

  • I am modified your code little bit. I am not run this code but hope it helps you.

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

    Create the PFObject outside the loop.Set all the PFFile object to the PFObject inside the loop.

    After loop, save the PFObject. It is better to use the method :

            [obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
            {
                   //Check whether the upload is succeeded or not
            }];