[英]RLMResults:allObjects crash (iOS Objective-C)
I have old project written on Objective-C. 我有一个写在Objective-C上的老项目。 Need to do migration to Realm.
需要迁移到Realm。
I created several objects/classes inheritance from RLMObject
. 我从
RLMObject
创建了几个对象/类继承。 When I do fetching objects only with one main object type ( ConnectionRealm
) - working fine, but if I do add (only add, not include, not use) to project two or more another classes (inheritance from RLMObject
), like as FloorRealm
class, APP crash on [ConnectionRealm allObjects]
without any errors. 当我仅使用一种主要对象类型(
ConnectionRealm
)来获取对象时-工作正常,但是如果我添加(仅添加,不包括,不使用)来投影两个或多个其他类(从RLMObject
继承),例如FloorRealm
类,APP在[ConnectionRealm allObjects]
上崩溃,没有任何错误。
Also ConnectionRealm
contains RLMArray
of FloorRealm
. 另外
ConnectionRealm
包含RLMArray
的FloorRealm
。 App still crashing. 应用仍然崩溃。 (Can`t solve and understand this few days.) Thanks.
(这几天无法解决和理解。)谢谢。
Connection Model: 连接型号:
#import <Foundation/Foundation.h>
#import <Realm/Realm.h>
#import "FloorRealm.h"
@interface ConnectionRealm : RLMObject
@property int connectionID;
@property NSString *name;
@property NSString *localIPAddress;
@property NSString *localPort;
@property NSString *remoteIPAddress;
@property NSString *remotePort;
@property NSString *userName;
@property NSString *password;
@property NSString *deviceID;
@property RLMArray <FloorRealm *> *floors;
- (instancetype)initWith:(NSString *)name
localIP:(NSString *)localIPAddress
localPort:(NSString *)lPort
remoteIP:(NSString *)remoteIPAddress
remotePort:(NSString *)rPort
userName:(NSString *)userName
password:(NSString *)password
deviceID:(NSString *)deviceID;
@end
#import "ConnectionRealm.h"
@implementation ConnectionRealm
- (instancetype)initWith:(NSString *)name
localIP:(NSString *)localIPAddress
localPort:(NSString *)lPort
remoteIP:(NSString *)remoteIPAddress
remotePort:(NSString *)rPort
userName:(NSString *)userName
password:(NSString *)password
deviceID:(NSString *)deviceID {
if (self = [super init]) {
self.connectionID = [self incrementID];
self.name = name;
self.localIPAddress = localIPAddress;
self.localPort = lPort;
self.remoteIPAddress = remoteIPAddress;
self.remotePort = rPort;
self.userName = userName;
self.password = password;
self.deviceID = deviceID;
}
return self;
}
+ (NSString *)primaryKey { return @"connectionID"; }
- (int)incrementID {
RLMResults *objects = [ConnectionRealm allObjects];
return self.connectionID = [[objects maxOfProperty:@"connectionID"] intValue] + 1;
}
@end
FloorModel: FloorModel:
#import <Realm/Realm.h>
@interface FloorRealm : RLMObject
@property int floorID;
@property NSInteger floorNumber;
@property NSString *floorName;
- (instancetype)initWith:(NSInteger)floorNumber floorName:(NSString *)name;
@end
RLM_ARRAY_TYPE(FloorRealm)
#import "FloorRealm.h"
@implementation FloorRealm
- (instancetype)initWith:(NSInteger)floorNumber floorName:(NSString *)name {
if (self = [super init]) {
self.floorID = [self incrementID];
self.floorNumber = floorNumber;
self.floorName = name;
}
return self;
}
+ (NSString *)primaryKey { return @"floorID"; }
- (int)incrementID {
RLMResults *objects = [FloorRealm allObjects];
return self.floorID = [[objects maxOfProperty:@"floorID"] intValue] + 1;
}
@end
[SOLVED] [解决了]
RLM_ARRAY_TYPE(FloorRealm)
need put on ConnectionRealm in .h after #includes. RLM_ARRAY_TYPE(FloorRealm)
放在.h中的ConnectionRealm中。 But in official docs written another. RLMArray <FloorRealm *><FloorRealm> *floors;
RLMArray <FloorRealm *><FloorRealm> *floors;
instead of @property RLMArray <FloorRealm *> *floors;
@property RLMArray <FloorRealm *> *floors;
I created test project with the same models and seed all errors. 我使用相同的模型创建了测试项目,并植入了所有错误。 Strange, but in original project Xcode not showing this errors.
奇怪,但在原始项目Xcode中未显示此错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.