如何在 Realm 谓词中正确使用协议定义的 属性?
How to properly use a protocol-defined property in Realm predicates?
我正在尝试使用 Realm 实现缓存系统。
根据类,缓存应该有不同的长度。因此,我定义了一个 StalenessChecking
协议:
@protocol StalenessChecking <NSObject>
@required
// nonatomic needed when using a getter
@property (readonly, nonatomic, getter=isStale) bool stale;
@optional
- (void) setStaleness: (NSTimeInterval) duration;
@end
和一个对象:
接口文件(DziObject.h)
#import "Realm.h"
#import "StalenessChecking.h"
@interface DziObject : RLMObject <StalenessChecking>
@property (readonly) NSDate* refresh;
@end
实现文件(DiObject.m)
#import 'DziObject.h'
@implementation DziObject
{
NSTimeInterval stalenessInterval;
}
@synthesize stale = _stale;
- (instancetype)init
{
self = [super init];
if (self) {
_refresh = [NSDate date];
stalenessInterval = 120.0;
}
return self;
}
- (bool) isStale {
return [[NSDate dateWithTimeInterval:stalenessInterval sinceDate:_refresh] timeIntervalSinceReferenceDate]< [[NSDate date] timeIntervalSinceReferenceDate];
}
- (void)setStaleness:(NSTimeInterval) duration
{
stalenessInterval = duration;
}
@end
然后我从 Facade 调用它们:
接口:
#import <Foundation/Foundation.h>
@interface SDK_Facade : NSObject
+ (void) createDziO;
@end
实施:
#import "SDK_Facade.h"
#import "DziObject.h"
@implementation SDK_Facade
+ (void) createDziO
{
DziObject *dziO = [[DziObject alloc] init];
// both work fine
if (dziO.isStale) {
NSLog(@"Is stale");
}
if (dziO.stale) {
NSLog(@"Is really stale");
}
// Query Realm for all results less than 2 minutes old
// TODO: -- currently crashes
RLMResults< DziObject *> * dzios = [DziObject objectsWhere:@"stale == %@", @YES];
NSLog(@"DziOs: %lu", (unsigned long)dzios.count);
// Persist your data easily
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
[realm addObject:dziO];
}];
// Queries should update in realtime
NSLog(@"DO: %lu", (unsigned long)dzios.count);
}
@end
我遇到了一个丑陋的崩溃:
Terminating app due to uncaught exception 'Invalid property name', reason: 'Property 'stale' not found in object of type 'DziObject'
您不能在模型 class 遵循的协议中定义 Realm 模型 属性。它必须在 class 定义本身中完成。
@interface DziObject : RLMObject <StalenessChecking>
@property NSDate* refresh;
@property (getter=isStale) bool stale;
@end
我正在尝试使用 Realm 实现缓存系统。
根据类,缓存应该有不同的长度。因此,我定义了一个 StalenessChecking
协议:
@protocol StalenessChecking <NSObject>
@required
// nonatomic needed when using a getter
@property (readonly, nonatomic, getter=isStale) bool stale;
@optional
- (void) setStaleness: (NSTimeInterval) duration;
@end
和一个对象:
接口文件(DziObject.h)
#import "Realm.h" #import "StalenessChecking.h" @interface DziObject : RLMObject <StalenessChecking> @property (readonly) NSDate* refresh; @end
实现文件(DiObject.m)
#import 'DziObject.h' @implementation DziObject { NSTimeInterval stalenessInterval; } @synthesize stale = _stale; - (instancetype)init { self = [super init]; if (self) { _refresh = [NSDate date]; stalenessInterval = 120.0; } return self; } - (bool) isStale { return [[NSDate dateWithTimeInterval:stalenessInterval sinceDate:_refresh] timeIntervalSinceReferenceDate]< [[NSDate date] timeIntervalSinceReferenceDate]; } - (void)setStaleness:(NSTimeInterval) duration { stalenessInterval = duration; } @end
然后我从 Facade 调用它们:
接口:
#import <Foundation/Foundation.h>
@interface SDK_Facade : NSObject
+ (void) createDziO;
@end
实施:
#import "SDK_Facade.h"
#import "DziObject.h"
@implementation SDK_Facade
+ (void) createDziO
{
DziObject *dziO = [[DziObject alloc] init];
// both work fine
if (dziO.isStale) {
NSLog(@"Is stale");
}
if (dziO.stale) {
NSLog(@"Is really stale");
}
// Query Realm for all results less than 2 minutes old
// TODO: -- currently crashes
RLMResults< DziObject *> * dzios = [DziObject objectsWhere:@"stale == %@", @YES];
NSLog(@"DziOs: %lu", (unsigned long)dzios.count);
// Persist your data easily
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
[realm addObject:dziO];
}];
// Queries should update in realtime
NSLog(@"DO: %lu", (unsigned long)dzios.count);
}
@end
我遇到了一个丑陋的崩溃:
Terminating app due to uncaught exception 'Invalid property name', reason: 'Property 'stale' not found in object of type 'DziObject'
您不能在模型 class 遵循的协议中定义 Realm 模型 属性。它必须在 class 定义本身中完成。
@interface DziObject : RLMObject <StalenessChecking>
@property NSDate* refresh;
@property (getter=isStale) bool stale;
@end