Reputation: 33080
Every restaurant must have Latitude and Longitude. However, every LatitudeLongitude object must not have "a restaurant"
So I have a one way relationship and generate a compiler warning.
What's the catch?
Should I use fetchRelationship instead? But what is fetchRelationship?
Why is it one way? Why does it have predicate? Why it is called fetch? I read the documentation and doesn't seem to get what it really is.
Upvotes: 1
Views: 1253
Reputation: 1450
OK, super-old question, but here's how I'd be solving it now.
Not all relationships have to be filled. Keep your inverse (two-way) relationships but you don't necessarily have to link them.
+ (void)createRestaurantWithCompletion:(void (^)(BOOL, NSError *))completion {
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
Restaurant *localRestaurant;
LatitudeLongitude *localLatLng1;
LatitudeLongitude *localLatLng2;
restaurant = [Restaurant MR_createInContext:localContext];
localLatLng1 = [LatitudeLongitude MR_createInContext:localContext];
localLatLng2 = [LatitudeLongitude MR_createInContext:localContext];
restaurant.latLng = localLatLng1;
} completion:completion];
}
By the time completion
is called, both localLatLng1
and localLatLng2
exist, one is linked to a restaurant, one isn't.
Of course this method makes no sense in this form, it's just to prove the point that you can create objects without having to satisfy their relationships.
Z.
Upvotes: 1
Reputation: 7303
You should use fetched properties (which are an collection of managed objects satisfying a specified fetch predicate) to represent one-way relationships. More information here.
Update
You should probably use attributes in your case, otherwise you would add a fetched property to the Restaurant
entity, set it's destination entity to LatitudeLongitude
and store lattitude and longitude key-value pairs in its userInfo
dictionary. Its fetch predicate would look like this:
($FETCH_SOURCE.longitude LIKE [c] $FETCHED_PROPERTY.userInfo.longitude) AND ($FETCH_SOURCE.latitude LIKE [c] $FETCHED_PROPERTY.userInfo.latitude)
Upvotes: 2