0
我有舊的項目寫在Objective-C上。需要遷移到Realm。RLMResults:allObjects崩潰(iOS Objective-C)
我從RLMObject
創建了幾個對象/類繼承。當我只使用一個主要對象類型(ConnectionRealm
)獲取對象時 - 工作正常,但如果我添加(僅添加,不包含,不使用)以投影兩個或更多其他類(繼承自RLMObject
),如FloorRealm
類,應用程序崩潰[ConnectionRealm allObjects]
沒有任何錯誤。
還有ConnectionRealm
包含RLMArray
的FloorRealm
。應用程序仍然崩潰。 (這幾天無法解決和理解。)謝謝。
連接型號:
#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:
#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