Start a new topic

A One-To-Many Relationship on Kinvey

Hello, I have spent hours trying to implement a one to many relationship using kinvey... I couldn't find one example on the tutorial section doing this... I have followed the guide here as best as I can (http://devcenter.kinvey.com/ios/guides/datastore#RelationalData) but even that is extremely vague...



Here Is what I am trying to accomplish, I have a User Class, and a League Class... A User has many leagues, but a league has only one user.



Part of my User.h (I am subclassing KCSUser,I am not sure if that matters, again there was not one piece of documentation show this)



@interface User : KCSUser

@property (nonatomic, retain)NSString*entityId;

@property (nonatomic, retain) NSString * firstName;

@property (nonatomic, retain) NSString * lastName;

@property (nonatomic, copy) NSMutableSet *leagues;

@end



Part of my User.m



@implementation User

- (NSDictionary *)hostToKinveyPropertyMapping

{

return @{

@"entityId" : KCSEntityKeyId, //the required _id field

@"firstName" : @"firstName",

@"lastName" : @"lastName",

@"leagues" : @"leagues",

};

}

+ (NSDictionary *)kinveyPropertyToCollectionMapping

{

return @{@"leagues" /* backend field name */ : @"League" /* collection name for invitations */};

}

+(NSDictionary *)kinveyObjectBuilderOptions

{

// reference class map - maps properties to objects

return @{ KCS_REFERENCE_MAP_KEY : @{ @‌ " : [League class]}};

}

@end



Here is part of my League.h



@interface League : NSObject

@property (nonatomic, retain)NSString*entityId;

@property (nonatomic, retain) KCSUser *relationshipOwner;

@end



Here is part of my League.m



-(NSDictionary *)hostToKinveyPropertyMapping

{

return @{

@"entityId" : KCSEntityKeyId, //the required _id field

@"relationshipOwner" : @"relationshipOwner",

};

}

+ (NSDictionary *)kinveyPropertyToCollectionMapping

{

return @{ @"invitee" : KCSUserCollectionName };

}





And finally, here is where I perform the save.



League *newLeague = [[League alloc]init];

newLeague.relationshipOwner = [KCSUser activeUser];

_store = [KCSAppdataStore storeWithOptions:@{ KCSStoreKeyCollectionName : @"League",

KCSStoreKeyCollectionTemplateClass : [League class],

}];

[_store saveObject:newLeague withCompletionBlock:^(NSArray *objectsOrNil, NSError *errorOrNil) {

if (errorOrNil != nil) {

//save failed, show an error alert

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Save failed", @"Save Failed")

message:[errorOrNil localizedFailureReason] //not actually localized

delegate:nil

cancelButtonTitle:NSLocalizedString(@"OK", @"OK")

otherButtonTitles:nil];

[alertView show];

} else {

//save was successful

NSLog(@"Successfully saved event (id='%@').", [objectsOrNil[0] kinveyObjectId]);

}

} withProgressBlock:nil];



I get the error "No method in body"


Oops, it would help if I linked correctly. It's fixed now!
Hi Caroline thank you for the response.I have seen that first blog post, the second link you provided seems to be a dead link and It takes me to a blank screen. It makes things 100x easier if you just see real working code. Hopefully, you guys can get some tutorials soon.
Hi Geiger, you bring up a good point that the tutorials don't cover data modeling very well. We put out a couple of blog posts that touch on the topic. [Here](http://www.kinvey.com/blog/3732/data-modeling-in-kinvey "Here") and [here](http://www.kinvey.com/blog/71/how-to-model-data-relationships-in-your-kinvey-backend "here").



Hopefully those can help out a bit while I get someone from our engineering team to help take a look to answer your question.
Login or Signup to post a comment