Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Diary App Using Core Data Inserting and Saving Data Inserting Data into a Managed Object Context

Download an image file from Parse and save it to Data Stack after download complete.

In such a scenario, i should wait for getDataInBackgroundWithBlock:^(NSData *data, NSError *error) method to finish its job before i save the file to Data Stack as a binary data property.

I wanted to use a non-void method for file download operations, but when i try to return the binary data i get an error:

Incompatible block pointer types sending 'PFFile *(^)(NSData *strong, NSError *strong)' to parameter of type 'PFDataResultBlock' (aka 'void (^)(NSData *strong, NSError *strong)')

Any idea? How can i manage to do this?

My code:

CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
            CardData *cardData = [NSEntityDescription insertNewObjectForEntityForName:@"CardData" inManagedObjectContext:coreDataStack.managedObjectContext];
...
cardData.cardVisual = [self fileDataForStackFromParseFile: [dict objectForKey:@"cardVisual"]];
...
[coreDataStack saveContext];
...
-(NSData*) fileDataForStackFromParseFile:(NSData*)data {

    PFFile *userImageFile = [PFFile fileWithData:data];
    [userImageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (error) {
            NSLog(@"zzbamm! %@",error);
        } else {
            return userImageFile; //the data for saving to stack
        }
    }];
}

Thanks indeed.

1 Answer

Try returning the data parameter that is passed in the completion block.

-(NSData*) fileDataForStackFromParseFile:(NSData*)data {

    PFFile *userImageFile = [PFFile fileWithData:data];
    [userImageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (error) {
            NSLog(@"zzbamm! %@",error);
        } else {
            return data; //the data for saving to stack
        }
    }];
}

Thank you Stephen, but i get the same error. I also tried renaming "data" in the container function, result is the same.

Looking again at the line of code where you're setting your cardVisual property to the result of your 'fileDataForStack...' method, make sure that the varType of that object in the dictionary you're passing in is actually of type NSData. It could very well be a string you're passing in instead. Try logging out the result of that 'data' response to see if it's even working. If not, you might want to do what I mentioned and make sure [dict objectForKey:@"cardVisual"] is typedef NSData.