繁体   English   中英

RLMResults:allObjects崩溃(iOS Objective-C)

[英]RLMResults:allObjects crash (iOS Objective-C)

我有一个写在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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM