Search code examples
iosarraysuitableviewparse-platformwatchkit

Get an array from Parse query


I'm using Parse to create this table view, and am trying to figure out how to get the Parse table data into an array, so I can pass it into the WatchKit InterfaceController to show the exact same thing?

So I want to show in the WatchKit interface exactly what shows in the iPhone interface.

Here is what I have, let me know if I can add anything that would be helpful:

TableVC.m:

- (id)initWithCoder:(NSCoder *)aCoder
{
    self = [super initWithCoder:aCoder];
    if (self) {
        self.parseClassName = @"na";
        self.textKey = @"dateTime";
        self.pullToRefreshEnabled = YES;
        self.paginationEnabled = NO;
    }
    return self;
}
- (PFQuery *)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

    return query;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
    static NSString *simpleTableIdentifier = @"RecipeCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    UILabel *homeLabel = (UILabel*) [cell viewWithTag:101];
    homeLabel.text = [object objectForKey:@"test"];

    UILabel *dateLabel = (UILabel*) [cell viewWithTag:102];
    dateLabel.text = [object objectForKey:@"dateTime"];

    return cell;
}

Parse data:

enter image description here

TableVC.m:

enter image description here

I already have the basic WatchKit files and Storyboard set up. I hard coded an array to test that it was generally working. But now I just need to get the data from Parse into there, and not sure if I need to do a query and then turn that into a public array?

EDIT:

Here is my query:

PFQuery *query2 = [PFQuery queryWithClassName:@"nba"];
[query2 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // The find succeeded.
        NSLog(@"Objects 2: %@", objects);
        }
    } else {
        // Log details of the failure
        NSLog(@"Error 2: %@ %@", error, [error userInfo]);
    }
}];

Here is my NSLog:

NSLog(@"Objects 2: %@", objects);

Console:

2015-02-09 21:06:30.845 SimpleTable[8373:1284663] Objects 2: (
    "<na: 0x7ff3f8e40880, objectId: cOrjeAmwJh, localId: (null)> {\n    away = Cav;\n    date = \"04/19/2015\";\n    dateTime = \"April 19, 2015, 16:00\";\n    gNumber = 1;\n    home = Bul;\n    matup = \"Ca\";\n    ro = \"Ro \";\n    test = \"Test 2\";\n    tv = T;\n}",

Solution

  • If you need the array, fetch it asynchronously in a method outside of the queryForTable method, get it like this:

    PFQuery *query = [self queryForTable];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // objects is the array for this table
            NSMutableArray *array = [@[] mutableCopy];
            for (PFObject *object in objects) {
                NSLog(@"we got an object with dateTime = %@", [object objectForKey:@"dateTime"]);
                [array addObject:[object objectForKey:@"dateTime"]];
                // you can prove this with any of your keys: away, number, home, mat up, etc.
            }
        }
    }];