1

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.

Moshe
  • 57,511
  • 78
  • 272
  • 425
user4951
  • 32,206
  • 53
  • 172
  • 282

2 Answers2

2

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)
David
  • 7,310
  • 6
  • 41
  • 63
  • Really? But what predicate would LatitudeLongitude object have? – user4951 Apr 10 '11 at 18:24
  • Really, then what predicate should the LattitudeLongitude object has? Also each Restaurant should only have one LattitudeLongitude object. In fact, I probably should have made this an attribute rather than a relationship. However, I am just curious. – user4951 Apr 10 '11 at 18:25
  • You're right, if you have no need to have `LatitudeLongitude` objects might as well use attributes for latitude and longitude. I've updated my answer with what you would do if you choose to use fetched properties. – David Apr 11 '11 at 00:11
  • What does [c] means in your predicate? – user4951 Apr 11 '11 at 03:30
  • It makes the comparison case-insensitive. – David Apr 11 '11 at 18:46
  • Why use equal, why not use like – user4951 Apr 14 '11 at 05:01
  • Sorry why use Like why not use equal? – user4951 Apr 15 '11 at 10:27
  • LIKE is used for string comparison, look at the [Predicate Format String Syntax](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html%23//apple_ref/doc/uid/TP40001795-CJBDBHCB) for more information. – David Apr 15 '11 at 19:38
1

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.

Zoltán
  • 1,422
  • 17
  • 22