我有一个写在Objective-C上的老项目。需要迁移到Realm。

我从RLMObject创建了几个对象/类继承。当我仅使用一种主要对象类型(ConnectionRealm)获取对象时-可以正常工作,但是如果我确实添加(仅添加,不包括,不使用)以投影两个或多个其他类(来自RLMObject的继承),像FloorRealm类一样,APP在[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


地板型号:

#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

最佳答案

[解决了]


RLM_ARRAY_TYPE(FloorRealm)需要在#includes之后放入.h中的ConnectionRealm。但是在官方文档中又写了一个。
另外:@property RLMArray <FloorRealm *><FloorRealm> *floors;而不是@property RLMArray <FloorRealm *> *floors;


我使用相同的模型创建了测试项目,并植入了所有错误。奇怪,但在原始项目Xcode中未显示此错误。

关于ios - RLMResults:allObjects崩溃(iOS Objective-C),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46399588/

10-11 19:47