Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've done something similar to Shortseller, but without the use of categories.</p> <p><a href="http://i40.tinypic.com/x3ryp.png">alt text http://i40.tinypic.com/x3ryp.png</a></p> <p><code>InternationalBook</code> and <code>LocalizedBook</code> are both custom managed objects with a one-to-many relationship (one international book to many localised books).</p> <p>In the implementation of <code>InternationalBook</code>, I've added a custom accessor for <code>title</code>:</p> <pre><code>- (NSString *)title { [self willAccessValueForKey:@"title"]; NSString *locTitle = nil; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"locale==%@", [DataManager localeString]]; NSSet *localizedSet = [self.localizedBook filteredSetUsingPredicate:predicate]; if ([localizedSet count] &gt; 0) { locTitle = [[localizedSet valueForKey:@"localizedTitle"] anyObject]; } [self didAccessValueForKey:@"title"]; return locTitle; } </code></pre> <p><code>[DataManager localeString]</code> is a class method which returns the user's language and country code: <code>en_US</code>, <code>fr_FR</code>, etc. See documentation on <code>NSLocale</code> for details.</p> <p>See the "Custom Attribute and To-One Relationship Accessor Methods" section of the Core Data Programming Guide for an explanation of <code>willAccessValueForKey:</code> and <code>didAccessValueForKey:</code>.</p> <p>When populating the data, I grab a string representing the user's current locale (<code>[DataManager localeString]</code>), and store that along the with localised book title in a new <code>LocalizedBook</code> object. Each <code>LocalizedBook</code> instance is added to an <code>NSMutableSet</code>, which represents the one-to-many relationship.</p> <pre><code>NSMutableSet *bookLocalizations = [internationalBook mutableSetValueForKey:@"localizedBook"]; // internationalBook is an instance of InternationalBook // set the values for locale and localizedTitle LocalizedBook *localizedBook = (LocalizedBook *)[NSEntityDescription insertnNewObjectEntityForName:@"LocalizedBook" inManagedObjectContext:self.bookMOC]; localizedBook.locale = [DataManager localeString]; localizedBook.localizedTitle = theLocalizedTitle; // assume theLocalizedTitle has been defined. [bookLocalizations addObject:localizedBook]; [bookLocalizations setValue:localizedBook forKey:@"localizedBook"]; </code></pre> <p>Since the localised titles are being stored in the <code>LocalizedBook</code> managed object, you can make the <code>title</code> attribute a transient, but if you do that you can't use <code>title</code> in a predicate.</p> <p>The nice thing about this approach is that the implementation of the to-many relationship is transparent to any consumers. You simply request <code>internationalBook.title</code> and the custom accessor returns the appropriate value based on the user's locale behind the scenes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload