Rayen Kamta
Rayen Kamta

Reputation: 214

Combind parse queries and add to seperate arrays

How can I combine multiple Parse Queries? I want to query the column sclink and songTitle from parse.com then add each to its own array.

Also how to save query locally and call it? IF else statment or something:

NSMutableArray *trackList = [[NSMutableArray alloc] init];
PFQuery *queryTracks = [PFQuery queryWithClassName:@"liveRadioPL"];
NSArray *objects = [queryTracks findObjects]; // Online PFQuery results
[PFObject pinAllInBackground:objects];

[queryTracks selectKeys:@[@"scLink"]];
[queryTracks findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // The find succeeded.
        totalTracks = (int)objects.count;
        NSLog(@"Successfully retrieved %lu Tracks.", (unsigned long)objects.count);
        // Do something with the found objects
        for (PFObject *object in objects) {

            [trackList addObject:[NSString stringWithFormat:@"%@", [object objectForKey:@"scLink"]]];


        }
    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

trackListArray = trackList;


NSMutableArray *trackTitles = [[NSMutableArray alloc] init];

PFQuery *queryTitles = [PFQuery queryWithClassName:@"liveRadioPL"];
NSArray *objectsTitle = [queryTitles findObjects]; // Online PFQuery results
[PFObject pinAllInBackground:objects];

[queryTracks selectKeys:@[@"songTitle"]];
[queryTracks findObjectsInBackgroundWithBlock:^(NSArray *objectsTitle, NSError *error) {
    if (!error) {
        // The find succeeded.
        totalTitles = (int)objectsTitle.count;
        NSLog(@"Successfully retrieved %lu Titles.", (unsigned long)objectsTitle.count);
        // Do something with the found objects
        for (PFObject *object in objects) {

            [trackTitles addObject:[NSString stringWithFormat:@"%@", [object objectForKey:@"songTitle"]]];


        }
    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

Upvotes: 0

Views: 51

Answers (1)

Michael E
Michael E

Reputation: 674

I'm not sure your logic really makes sense - you're using 4 API requests when all you need is 1 API request. Also, Jacob is right, you're filling an array from a background thread and as a result the main thread will see it as empty.

I think I understand what you're trying to do - try this

PFQuery *queryTracks = [PFQuery queryWithClassName:@"liveRadioPL"];
// use includeKey if slink and songTitle are pointers to other Parse classes
// from the context of your question you probably don't need to use includeKey
[queryTracks includeKey:"scLink"];
[queryTracks includeKey:"songTitle"]; 
NSArray *objects = [queryTracks findObjects];

NSMutableArray* scLinks = [[NSMutableArray alloc] init];
NSMutableArray* songTitles = [[NSMutableArray alloc] init];

for (PFObject* object in objects) {
[scLinks addObject:object[@"scLink"]];
[songTitles addObject:object[@"songTitles"]];
}

I hope this helps, good luck!

Upvotes: 1

Related Questions