gpt4 book ai didi

cocoa-touch - Core Data 自定义访问器甚至不会被调用

转载 作者:行者123 更新时间:2023-12-04 02:51:18 25 4
gpt4 key购买 nike

我有一个我试图在运行时设置的 Core Data 属性,其值来自另一个属性。然而,奇怪的是,我构建的自定义访问器似乎从未被调用过。

属性(property),seasonNameVisible ,仅从带有搜索字段中的术语的谓词中调用,如下所示:

// 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];
}

如果我将该属性更改为 seasonName ,谓词的那部分返回结果就好了,所以我不怀疑谓词或任何其他搜索代码。

我的计划是导出 seasonNameVisible NSString 来自 seasonName在运行时。所以,我修改了 Season NSManagedObject 子类使用原始访问器覆盖访问器和 setter 。但据我所知,我的访问者永远不会被调用。

这是标题/界面:
//
// 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

...和实现:
//
// 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

我已经阅读了 Apple 的文档,并在 StackOverflow 上搜索了自定义访问器方法的帮助,我认为我的代码是正确的(这是我第一次尝试使用原始访问器或覆盖 NSManagedObject 方法,所以我有点超出我通常的深度),但即使我在它上面放置了一个断点,它似乎也永远不会被调用。

最佳答案

您要求基于延迟加载的属性获取核心数据。我不认为这会奏效。您需要在设置季节名称时设置您的seasonNameVisible,或者,这可能更有意义,将您的季节名称的存储分为前缀(您正在删除以提供可见名称)和真实名称姓名。
在评论中的附加信息之后,我建议如下:

  • 将您的季节分成两个属性,一个季节编号 (int) 和季节名称
  • 按季节编号对您的提取请求进行排序
  • section name key path 是对象上的一个新的只读属性,它返回一个由数字和名称组成的字符串

  • 在更改与 FRC 一起使用的提取请求时,您在评论中看到的错误是正常的。从模拟器中删除应用程序并重新构建,它会消失,或者在开发时使用 nil 缓存。 FRC 永久存储其缓存(例如在运行之间),因此任何更改都会扰乱它。
    段名键路径可以是任何你喜欢的键路径或属性名,只要排序相同即可。来自 NSFetchedResultsController 的文档:

    sectionNameKeyPath

    A key path on result objects that returns the section name. Pass nil to indicate that the controller should generate a single section.The section name is used to pre-compute the section information.If this key path is not the same as that specified by the first sort descriptor in fetchRequest, they must generate the same relative orderings. For example, the first sort descriptor in fetchRequest might specify the key for a persistent property; sectionNameKeyPath might specify a key for a transient property derived from the persistent property.


    所以,你会有一个 Season具有两个持久属性的对象, seasonNumberseasonName .您可以按季节编号对提取请求进行排序。您的部分名称键路径(用于获取可能是具有 season 关系的剧集)将是 @"season.seasonSectionName" ,实现如下 - 您的托管对象模型没有变化,只是对您的 Season 对象进行了更改:
    季节.h:
    @property(nonatomic,readonly) NSString *seasonSectionName;
    季节.m:
    -(NSString*)seasonSectionName
    {
    return [NSString stringWithFormat:@"%d - %@",self.seasonNumber,self.seasonName];
    }
    您真正要做的就是用另一个属性装饰季节编号。

    关于cocoa-touch - Core Data 自定义访问器甚至不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9339167/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com