gpt4 book ai didi

ios - RLM 结果 :allObjects crash (iOS Objective-C)

转载 作者:行者123 更新时间:2023-11-29 00:11:33 25 4
gpt4 key购买 nike

我有一个用 Objective-C 编写的旧项目。需要迁移到 Realm。

我从 RLMObject 继承了几个对象/类。当我仅使用一种主要对象类型 (ConnectionRealm) 获取对象时 - 工作正常,但如果我确实添加(仅添加,不包括,不使用)以投影两个或更多其他类(继承自RLMObject),就像 FloorRealm 类一样,APP 在 [ConnectionRealm allObjects] 上崩溃,没有任何错误。

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

连接模型:

#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

最佳答案

[已解决]

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com