简体   繁体   English

WatchKit,核心数据和表格

[英]WatchKit, Core Data and tables

I have a published app and now I want to build in a watch extension that will just present a table on the watch populated with data from my core data store. 我有一个已发布的应用程序,现在我想构建一个手表扩展程序,该扩展程序将在手表上显示一个表,该表中填充了我的核心数据存储区中的数据。 Very simple for now. 现在非常简单。 So I basically tried to use the code I have in my app tableviewcontroller modified for the watch extension. 因此,我基本上尝试使用在我的应用程序tableviewcontroller中为watch扩展修改的代码。 I have the app group in place, and created a core data class that both the app and the watch extension can access. 我已经准备好了应用程序组,并创建了一个核心数据类,该应用程序和watch扩展都可以访问。 And although that seems to be working fine in the watch extension based on the debug logs, I am not getting any data in the watch table when I run the app on the watch. 并且尽管这在基于调试日志的手表扩展中似乎还可以正常工作,但是在手表上运行应用程序时,手表表中没有任何数据。 The code and debug logs are below: 代码和调试日志如下:

InterfaceController.h InterfaceController.h

#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
#import "CoreDataHelper.h"

@interface InterfaceController : WKInterfaceController

@property (strong, nonatomic) IBOutlet WKInterfaceTable *table;
@property (strong, nonatomic) NSArray *objects;

@end

InterfaceController.m: InterfaceController.m:

#import "InterfaceController.h"
#import "ScheduleTableRow.h"

@interface InterfaceController()

@end

@implementation InterfaceController

@synthesize objects; 

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

// Configure interface objects here.

NSManagedObjectContext * managedContext = [[CoreDataHelper sharedHelper] context];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Cschedule" inManagedObjectContext:managedContext];

NSManagedObject *object = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:managedContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

[fetchRequest setEntity:entity];

NSArray *myResults = [managedContext executeFetchRequest:fetchRequest error:nil];

self.objects = myResults;

NSLog(@"Results count: %lu", (unsigned long)myResults.count);

[self.table setNumberOfRows:myResults.count withRowType:@"ScheduleTableRow"];

for (object in myResults) {

    ScheduleTableRow *scheduleRow = [self.table rowControllerAtIndex:[myResults indexOfObject:object]];
    [scheduleRow.name setText:[NSString stringWithFormat:@"%@",[object valueForKey:@"day"]]];
    [scheduleRow.date setText:[NSString stringWithFormat:@"%@",[object valueForKey:@"date"]]];
}

}

- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
[super willActivate];
}

- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
[super didDeactivate];
}

@end

DEBUG LOG: 调试日志:

2015-11-17 15:07:12.143 canada2016Watch Extension[89456:1406057] Running CoreDataHelper 'init'
2015-11-17 15:07:12.148 canada2016Watch Extension[89456:1406057] Running CoreDataHelper 'setupCoreData'
2015-11-17 15:07:12.148 canada2016Watch Extension[89456:1406057] Running CoreDataHelper 'loadStore'
2015-11-17 15:07:12.148 canada2016Watch Extension[89456:1406057] Running CoreDataHelper 'storeURL'
2015-11-17 15:07:12.148 canada2016Watch Extension[89456:1406057] Running CoreDataHelper 'applicationStoresDirectory'
2015-11-17 15:07:12.148 canada2016Watch Extension[89456:1406057] Running CoreDataHelper 'applicationDocumentsDirectory'
2015-11-17 15:07:12.159 canada2016Watch Extension[89456:1406057]    Successfully added store: <NSSQLCore: 0x7b8692b0> (URL:   file:///Users/barry/Library/Developer/CoreSimulator/Devices/E7C26A87-B22C-45C5-9AE3-18CB24393EA9/data/Containers/Data/PluginKitPlugin/6717F002-7F1B-44EB-8C9F-861B6CF01E08/Documents/Stores/group.org.bicsi.canada2016app)
2015-11-17 15:07:12.160 canada2016Watch Extension[89456:1406057] Results count: 1

Maybe you misunderstood the rowControllerAtIndex method. 也许您误解了rowControllerAtIndex方法。 This method is the actual place where you add rows to your table. 此方法是在表中添加行的实际位置。 Instead, you reference the index as indexOfObject although the table does not have any objects yet. 而是将索引引用为indexOfObject尽管该表还没有任何对象。

Instead, you should loop through your indexes, create an item for each row and populate it with the appropriate object. 相反,您应该遍历索引,为每行创建一个项目,并使用适当的对象填充它。

for (int i = 0; i < self.objects.count; i++) {
   ScheduleTableRow *scheduleRow = [self.table rowControllerAtIndex:i];
   NSManagedObject *item = self.objects[i];
   scheduleRow.name.text = [item valueForKey:@"day"];
   scheduleRow.date.text = [item valueForKey:@"date"];
}

Much more elegant in Swift Swift更优雅

for (idx, object) in self.objects.enumerate() {
   let scheduleRow = self.table.rowControllerAtIndex(idx)
   scheduleRow.name.text = object.valueForKey("day")
   scheduleRow.date.text = object.valueForKey("date")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM