2017-09-25 46 views
0

我有旧的项目写在Objective-C上。需要迁移到Realm。RLMResults:allObjects崩溃(iOS Objective-C)

我从RLMObject创建了几个对象/类继承。当我只使用一个主要对象类型(ConnectionRealm)获取对象时 - 工作正常,但如果我添加(仅添加,不包含,不使用)以投影两个或更多其他类(继承自RLMObject),如FloorRealm类,应用程序崩溃[ConnectionRealm allObjects]没有任何错误。

还有ConnectionRealm包含RLMArrayFloorRealm。应用程序仍然崩溃。 (这几天无法解决和理解。)谢谢。

连接型号:

#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 

回答

1

[解决]

  1. RLM_ARRAY_TYPE(FloorRealm)需要穿上ConnectionRealm在.H的#includes后。但在官方文档写了另一个。
  2. 另外:@property的RLMArray <FloorRealm *><FloorRealm> *floors;代替@property RLMArray <FloorRealm *> *floors;

我创建了相同型号的测试项目和种子的所有错误。奇怪,但在原来的项目Xcode没有显示这个错误。