1

I have an endpoint message from Google Cloud Endpoint that I would like to persist in iOS Core Data. The endpoint message look like this

.h

#if GTL_BUILT_AS_FRAMEWORK
  #import "GTL/GTLObject.h"
#else
  #import "GTLObject.h"
#endif

@class GTLFarmercloudendpointChicken;
@class GTLFarmercloudendpointPig;
@class GTLFarmercloudendpointCow;

// ----------------------------------------------------------------------------
//
//   GTLFarmercloudendpointFarmer
//

@interface GTLFarmercloudendpointFarmer : GTLObject
@property (retain) NSArray *chickens;  // of GTLFarmercloudendpointChicken
@property (retain) NSArray *pigs;  // of GTLFarmercloudendpointPig
@property (retain) NSArray *cows;  // of GTLFarmercloudendpointCow
@property (copy) NSString *biography;
@property (retain) NSNumber *userid;  // longLongValue
@property (copy) NSString *username;
@end

.m

/* This file was generated by the ServiceGenerator.
 * The ServiceGenerator is Copyright (c) 2014 Google Inc.
 */

//
//  GTLFarmercloudendpointFarmer.m
//

// ----------------------------------------------------------------------------
// NOTE: This file is generated from Google APIs Discovery Service.
// Service:
//   Farmercloudendpoint/1
// Description:
//   Farmer API
// Classes:
//   GTLFarmercloudendpointFarmer (0 custom class methods, 6 custom properties)

#import "GTLFarmercloudendpointFarmer.h"

#import "GTLFarmercloudendpointChicken.h”
#import "GTLFarmercloudendpointCow.h”
#import "GTLFarmercloudendpointPig.h”

// ----------------------------------------------------------------------------
//
//   GTLFarmercloudendpointFarmer
//

@implementation GTLFarmercloudendpointFarmer
@dynamic chickens, pigs, cows, biography, userid, username;

+ (NSDictionary *)arrayPropertyToClassMap {
  NSDictionary *map =
    [NSDictionary dictionaryWithObjectsAndKeys:
      [GTLFarmercloudendpointChicken class], @“chickens”,
      [GTLFarmercloudendpointPig class], @“pigs”,
      [GTLFarmercloudendpointCow class], @“cows”,
      nil];
  return map;
}

@end

So that well meaning people do not misunderstand the question: these are messages not App-Engine datastore entities. This means: on the server, after I query the datastore (or a sql db, or whatever, really), I create java POJOs (my server is in Java) and these POJOs are sent to the client using Google Cloud Endpoint. So please avoid discussions of Core Data VS datastore as I saw in a similar question here.

On another note: observe that the message classes that Google Cloud Endpoint generate do not extend NSManagedObject. So a linking converter is missing so that I could persist my endpoint message in Core Data. Other than meticulously creating a linking for each message myself, any ideas how to accomplish this? For example imagine that class GTLFarmercloudendpointCow may itself contain an NSArray of Eggs. Manually creating a linker becomes daunting very quickly.

A similar problem exists in fact if I try to save the endpoint message as a file: in that case I need a NSCoder coder/encoder for each nested class in the message else I cannot use NSKeyedArchiver.

Community
  • 1
  • 1
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
  • If I could somehow have the ServiceGenerator generate my endpoints with encodeWithCoder and initWithCoder, or on the other hand as subclasses of NSManagedObject, things would be fine. Modifying the endpoints manually means doing it over again the each time I make even the most mundane of server edits that need new endpoints. – Katedral Pillon Sep 12 '14 at 22:36
  • Actually I just realize that even if I were to go ahead and manually make my endpoints subclasses of NSManagedObject, I would still not know how to save them to core data. This is because all the examples of core data I find require the entity to be instantiated as `Entry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"SomeEntry" inManagedObjectContext:[self managedObjectContext]];` and then have data passed to it one property at a time. There is no way to essential assign a NSManagedObject "from the server" to Core Data. – Katedral Pillon Sep 13 '14 at 00:32
  • Is there some way of doing this simple assignment and then still be able to call `[context save:&error];`? – Katedral Pillon Sep 13 '14 at 00:32
  • Purely FTR, consider just using parse.com. It's unbelievably easier. (note that apart from anything else, caching, offline-caching, etc, is entirely taken care of.) – Fattie Sep 21 '14 at 12:10

0 Answers0