Studio Symposium
Studio Symposium

Reputation: 287

Passing Data to Singleton iOS

I'm currently having some trouble with data getting lost when transferring from a ViewController to a subclass of PFFile. The data being passed is image data to upload to a users profile. Here's the code for selecting the image:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

// Dismiss controller
[picker dismissViewControllerAnimated:YES completion:nil];

// Resize image
_focusedImage.image = image;


NSData *imageData = UIImageJPEGRepresentation(image, 0.05f);
PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:imageData];
[[imageUpload uploadImage] setImagePFFile:imageFile];
}

The Log on imageFile in this view is printing out correctly. However, when I pass the data through to my singleton class imageUpload uploadImage This is what the data structure looks like:

+ (imageUpload *) uploadImage
{
static imageUpload*_sharedImageUpload = nil;
_sharedImageUpload = [[self alloc] init];
_sharedImageUpload.imageData = [[NSData alloc] init];

PFUser *user = [PFUser currentUser];
_sharedImageUpload.imagePFFile = [[PFFile alloc] init];

PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:_sharedImageUpload.imageData];


[imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (!error)
    {
        [user setObject:imageFile forKey:@"image"];
        [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!error)
            {
                NSLog(@"This should be the profile image upload");
            }
            else
            {
                NSLog(@"Something went wrong: %@", error);
            }
        }];
    }
}];

return _sharedImageUpload;
}

When I get to this point, the system just uploads a blank file (zero bytes) to Parse. The naming is right and its going in the right place on the database, but somewhere along the line the data in the file itself is being lost. I can't figure out why. Does anyone have any suggestions?

Upvotes: 0

Views: 150

Answers (1)

jefflovejapan
jefflovejapan

Reputation: 2121

It looks like you're confusing objects and methods. What you want is a singleton object that has a method / function that uploads your image. I think this is what you're looking for:

//ImageUploader.h

#import <Foundation/Foundation.h>

@interface ImageUploader : NSObject
+ (instancetype)uploader;
- (void)uploadImageFile:(PFFile *)aFile;
@end

//ImageUploader.m

#import "ImageUploader.h"

@implementation ImageUploader
+ (instancetype)uploader {
    static ImageUploader * _uploader = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _uploader = [[self alloc] init];
    });

    return _uploader;
}

-(void)uploadPFFile:(PFFile *)imageFile{
    [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error)
        {
            [user setObject:imageFile forKey:@"image"];
            [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!error)
                {
                    NSLog(@"This should be the profile image upload");
                }
                else
                {
                    NSLog(@"Something went wrong: %@", error);
                }
            }];
        }
    }];
}
@end

You invoke it by calling [[ImageUploader uploader]uploadImageFile:someFile].

Upvotes: 6

Related Questions