Core Data custom accessory not even called

I have a Core Data property that I am trying to set at runtime, with a value obtained from another property. However, with curiosity, the custom accessory that I built does not even seem evoked.

The seasonNameVisible property, called only from the predicate with terms from the search field, as shown here:

 // Add our search predicates for (NSString *term in searchTerms) { NSPredicate *searchTermPredicate = [NSPredicate predicateWithFormat:@"(episodeName contains[cd] %@) OR (fromSeason.seasonNameVisible contains[cd] %@) OR (fromSeason.fromSeries.seriesName contains[cd] %@)", term, term, term]; [subPredicates addObject:searchTermPredicate]; } 

If I changed this property to seasonName , this part of the predicate will return the result just fine, so I don't suspect a predicate or any other search code.

My plan is to infer seasonNameVisible NSString from seasonName at runtime. So, I changed the subclass of Season NSManagedObject to override accessor and setter using primitive accessors. But as far as I can tell, my accessory is never called.

Here is the header / interface:

 // // Season.h // #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @class Episode, Series; @interface Season : NSManagedObject @property (nonatomic, retain) NSNumber * seasonIndex; @property (nonatomic, retain) NSString * seasonName; @property (nonatomic, retain) NSString * seasonNameVisible; @property (nonatomic, retain) NSSet *episodes; @property (nonatomic, retain) Series *fromSeries; @property (nonatomic, retain) NSString * primitiveSeasonName; @property (nonatomic, retain) NSString * primitiveSeasonNameVisible; @end @interface Season (CoreDataGeneratedAccessors) - (void)addEpisodesObject:(Episode *)value; - (void)removeEpisodesObject:(Episode *)value; - (void)addEpisodes:(NSSet *)values; - (void)removeEpisodes:(NSSet *)values; @end // my additions @interface Season (PrimitiveAccessors) - (NSString *)primitiveSeasonName; - (NSString *)primitiveSeasonNameVisible; @end 

... and implementation:

 // // Season.m // #import "Season.h" #import "Episode.h" #import "Series.h" @implementation Season @dynamic seasonIndex; @dynamic seasonName; @dynamic seasonNameVisible; @dynamic episodes; @dynamic fromSeries; // my additions @dynamic primitiveSeasonName; @dynamic primitiveSeasonNameVisible; - (NSString *)seasonNameVisible { NSString *visible; [self willAccessValueForKey:@"seasonNameVisible"]; visible = [self primitiveValueForKey:@"seasonNameVisible"]; [self didAccessValueForKey:@"seasonNameVisible"]; if (visible != nil) { return visible; } else { [self willAccessValueForKey:@"seasonName"]; visible = [[self primitiveValueForKey:@"seasonName"] substringFromIndex:2]; [self didAccessValueForKey:@"seasonName"]; [self setSeasonNameVisible:visible]; return visible; } } - (void)setSeasonNameVisible:(NSString *)seasonNameVisible { [self willChangeValueForKey:@"seasonNameVisible"]; [self setPrimitiveValue:seasonNameVisible forKey:@"seasonNameVisible"]; [self didChangeValueForKey:@"seasonNameVisible"]; } @end 

I read Apple docs and looked for help using special StackOverflow access methods, and I think I have the correct code (this is the first time I have tried using primitive accessors or overriding NSManagedObject, so I'm lagging a bit behind my usual depth) but even when I put a breakpoint on it, it never calls a call.

+4
source share
1 answer

You are requesting master data based on a lazily loaded attribute. I do not think this will work. You either need to set your season to NameVisible when you set the season name, or, which may make more sense, allocate the storage of your season name to everything that is a prefix (which you delete to give a visible name), and whatever the real name is .

After additional information in the comments, I would advise the following:

  • divide the season into two attributes, season number (int) and season name
  • sort the sample request by season
  • section key key is a new read-only property on your object that returns a string consisting of a number and a name

The error you see in your comment is normal when changing the fetch request used with FRC. Remove the application from the simulator and rebuild and it will disappear, or use cache zero during development. FRC stores its cache permanently (for example, between runs), so any changes violate it.

The section name key key can be any key path or property name that you like if the sorting is the same. From the docs for NSFetchedResultsController:

sectionNameKeyPath

The key path to result objects that returns the name of the section. Pass nil to indicate that the controller should generate a single partition. The section name is used to pre-compute the section information. If this key path does not match the specified first sort descriptor in fetchRequest, they should generate the same relative orders. For example, the first sort descriptor in the fetchRequest file may specify a key for a constant property; sectionNameKeyPath can specify the key for a transient property derived from a persistent property .

So, you will have a Season object with two constant attributes seasonNumber and seasonName . You will sort the sample request by season. Your path to the title of your section (for selection, presumably for episodes related to Season ) will be @"season.seasonSectionName" , implemented as follows: no changes to your managed entity model will just change to your Season object:

Season.h:

 @property(nonatomic,readonly) NSString *seasonSectionName; 

Season.m:

 -(NSString*)seasonSectionName { return [NSString stringWithFormat:@"%d - %@",self.seasonNumber,self.seasonName]; } 

Everything that you really do decorates the season number with another property.

+1
source

Source: https://habr.com/ru/post/1397161/


All Articles