0

I have a Model class 'Employee'. It has the following fields:

@interface Employee : NSManagedObject
@property (nonatomic, retain) NSNumber * employeeID;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * age;
@property (nonatomic, retain) NSString * address;
@property (nonatomic, retain) NSString * designation;
@property (nonatomic, retain) NSString * teamName;
@property (nonatomic, retain) NSString * gender;
@property (nonatomic, retain) NSNumber * dateOfJoining;
@end

I need to pass an array of 'Employee' to the Watch app but only three fields : name, gender, designation. How do I do this? Should I create a new Model class which has only this three fields and share it between iPhone and Watch? Eg:

 @interface EmployeeData : NSManagedObject
    @property (nonatomic, retain) NSString * name;
    @property (nonatomic, retain) NSString * designation;
    @property (nonatomic, retain) NSString * gender;
    @end

And then should I serialise array of EmployeeData and send it as NSData to the watch?

user2189878
  • 257
  • 3
  • 10
  • Possible duplicate of [How To Share Data with Watch OS 2 to display in WKInterfaceTable when working with CoreData](http://stackoverflow.com/questions/35929366/how-to-share-data-with-watch-os-2-to-display-in-wkinterfacetable-when-working-wi) –  May 04 '16 at 08:05

1 Answers1

1

You can send data inside a dictionary as well. Although it cannot be your specific class, you can pass the NSString class for your name, designation, and gender. Possibly share common string keys for your dictionary with both your watch extension and your phone App so the watch knows the string keys to use.

WCSession Documentation

Watch: Asking for data from the watch when you need it with WCSession sendMessage:replyHandler. Sends a message to the phone app and receives its callback.

[[WCSession defaultSession] sendMessage:@{@"EmployeData": @(YES)} replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
    // Extract data
} errorHandler:^(NSError * _Nonnull error) {
    // Failure to reach phone   
}];

iPhone: A class must conform to WCSessionDelegate protocol. session:didReceiveMessage: receives message from watch and replies with a dictionary of data. Here you can supply your information in the form of a dictionary.

- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message replyHandler:(void (^)(NSDictionary<NSString *,id> * _Nonnull))replyHandler
{
    // Your case could be:
    NSDictionary * aDictionary = @{@"Employees" : @[ @{@"name" : @"Alfred", @"designation" : @"Alabama", @"gender": @"Male"}, @{@"name" : @"Joe", @"designation" : @"New York", @"gender" : @"Male"} ]};
    replyHandler(aDictionary);
}
Antonioni
  • 578
  • 1
  • 5
  • 15