2017-09-25 70 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沒有顯示這個錯誤。