iosiphonecore-datawatchkitwkinterfacetable

How can I reload the data in a WatchKit tableview?


I have an app for iPhone with a Tableview, whose data comes from CoreData.

enter image description here

The same data are also displayed in a watch app:

enter image description here

If I add a row from the iPhone app:

enter image description here

and I reload data in the Watch app:

enter image description here

I see the old rows empty!

enter image description here

If the stop the watch app and I start it again, everything appears correctly!

enter image description here

This is the code to fill the Tableview in the watch app

-(void)awakeWithContext:(id)context{
    [super awakeWithContext:context];
       [self loadTable];
}

-(void)loadTable{
    NSLog(@"loadTableData");
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Data"];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Data"
        inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortByDate = [[NSSortDescriptor alloc] initWithKey:@"sortedDateAndTime" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByDate, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    self.watchMArray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

  //  [self.watchTableView setRowTypes:self.watchMArray];
    [self.watchTableView setNumberOfRows:self.watchMArray.count withRowType:@"data"];

    for (NSInteger i = 0; i < self.watchMArray.count; i++)
    {
        WatchTableCell *cell = [self.watchTableView rowControllerAtIndex:i];
        NSManagedObject *data = [self.watchMArray objectAtIndex:i];
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
            //Background Thread
            UIImage *foto =[self loadImageFromData:[data valueForKey:@"imageData"]];
            dispatch_async(dispatch_get_main_queue(), ^(void){
                //Run UI Updates
            [cell.watchImage setImage:foto];
            [cell.watchDate setText:[NSString stringWithFormat:@"%@", [data valueForKey:@"dataEOra"] ]];
            });
        });
    }
}

This is the code I am currently using to reload it:

- (IBAction)reloadTable {        
    [self loadTable];
}

Where am I wrong?


Solution

  • In the link @leolobato posted, there is actually a workaround. It did solve my problem.

    https://devforums.apple.com/message/1098875#1098875

    when you change your rows just set them to =@"" first.

    For example, if your row has a row.Label and you are changing it, do row.Label.text = @"" then again right after row.Label.text =@"[Actual text]"