Reputation: 11749
I am trying to download some short sound file on Parse.com in an iOS application. The file has previously been saved using the following code:
NSData *soundData = [NSData dataWithContentsOfURL:myURL];
parse_Sound = [PFFile fileWithName:@"XXYYZZ"
data:soundData];
[parse_Sound saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!succeeded) {
NSLog(@"sound-upload NG”);
} else {
NSLog(@"sound-upload OK");
}];
}
}];
Seeing the message on the debugging console, it appearently works. Now what kind of code do I need to run to retrieve(download) the sound file? I have browsed the net, but found nothing clear and working.
Upvotes: 1
Views: 158
Reputation: 2972
To get data back from the server you need to need to run a query asking for that object but you haven't associated the uploaded file with a column in any Class yet. Uploading a PFFile is iOS is a two step process:
1) Upload the PFFile to the server
2) In the callback associated the PFFile with a column in a data object
NSData *soundData = [NSData dataWithContentsOfURL:myURL];
parse_Sound = [PFFile fileWithName:@"XXYYZZ"
data:soundData];
[parse_Sound saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!succeeded) {
NSLog(@"sound-upload NG”);
} else {
NSLog(@"sound-upload OK");
PFObject *soundStuff = [PFObject objectWithClassName:@"Sounds"];
soundStuff[@"soundFile"] = parse_Sound;
[soundStuff saveInBackground];
}];
}
}];
Now to get the data back you would run a query on the Sounds class that will have the sound data in the soundFile column:
PFQuery *query = [PFQuery queryWithClassName:@"Sounds"];
[query whereKey:@"someKey" equalTo:@"someValue"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
PFFile *soundFile = object[@"soundFile"];
NSData *soundData = [soundFile getData];
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
I haven't tested any of this code but it at least demonstrates the steps needed and should get you started.
Here are the examples from the documentation:
PFObject *jobApplication = [PFObject objectWithClassName:@"JobApplication"]
jobApplication[@"applicantName"] = @"Joe Smith";
jobApplication[@"applicantResumeFile"] = file;
[jobApplication saveInBackground];
Then to get the data back:
PFFile *applicantResume = anotherApplication[@"applicantResumeFile"];
NSData *resumeData = [applicantResume getData];
Notice that file
is being associated with the applicantResumeFile
column of the JobApplication
class so that the file data can be retrieved in queries.
Upvotes: 1
Reputation: 9912
You need to keep a reference to that file somewhere (ideally in a column of the PFObject
it belongs to).
If you don't keep a reference you're out of luck and you can't retrieve already uploaded files that have no association to any object.
I suggest you read through the Parse documentation for files on iOS https://www.parse.com/docs/ios_guide#files/iOS
Upvotes: 1