gpt4 book ai didi

ios - 在 Realm Swift 中过滤嵌套数组上的对象

转载 作者:行者123 更新时间:2023-11-28 12:22:06 25 4
gpt4 key购买 nike

我有 ChatConversationModel 的主要模型,它有一个数组属性 userAcitivities,它是 ChatUserAcitivityModel 对象的数组。我想从 Realm 获取 ChatConversationModel 对象,其中 userAcitivities 包含给定的 friendID。我尝试了几种不同的方法,但未能获得我想要的结果。

import Foundation
import RealmSwift
import ObjectMapper

class ChatConversationModel: Object, Mappable {

dynamic var id = ""
dynamic var typeIndex = ChatTypes.ChatType.oneToOne.index
var userAcitivities = List<ChatUserAcitivityModel>()

override class func primaryKey() -> String? {
return "id"
}

convenience required init?(map: Map) {
self.init()
}

func mapping(map: Map) {
if map.mappingType == .fromJSON {
id <- map["id"]
typeIndex <- map["typeIndex"]
userAcitivities <- (map["userAcitivities"], ListTransform<ChatUserAcitivityModel>())
} else {
id >>> map["id"]
typeIndex >>> map["typeIndex"]
userAcitivities >>> (map["userAcitivities"], ListTransform<ChatUserAcitivityModel>())
}
}

}

import Foundation
import RealmSwift
import ObjectMapper

class ChatUserAcitivityModel: Object, Mappable {

dynamic var userID = ""
/// This is necessary in order to know from what point to download the chat if the user deleted it.
/// If this property is 0.0, then there has never been a deletion.
dynamic var removedChatTimeStamp = 0.0

override class func primaryKey() -> String? {
return "userID"
}

convenience required init?(map: Map) {
self.init()
}

func mapping(map: Map) {
if map.mappingType == .fromJSON {
userID <- map["userID"]
removedChatTimeStamp <- map["removedChatTimeStamp"]
} else {
userID >>> map["userID"]
removedChatTimeStamp >>> map["removedChatTimeStamp"]
}
}

}

func getFriendChatConversationModel(_ friendID: String) {
let realm = try! Realm()
let chatConversationModels = realm.objects(ChatConversationModel.self).filter("typeIndex = %@", ChatTypes.ChatType.oneToOne.index)
let friend = chatConversationModels.filter { $0.userAcitivities.filter { $0.userID == friendID } }
}

目前我正在做以下事情并且它对我有用,但我想找到表达这一点的最佳方式:

 func getFriendChatConversationModel(_ friendID: String) -> ChatConversationModel? {
let realm = try! Realm()
let chatConversationModels = realm.objects(ChatConversationModel.self).filter("typeIndex = %@", ChatTypes.ChatType.oneToOne.index)

var friendChatConversationModel: ChatConversationModel?

for chatConversationModel in chatConversationModels {
if chatConversationModel.userAcitivities.contains(where: { (chatUserAcitivityModel) -> Bool in
chatUserAcitivityModel.userID == friendID
}) {
friendChatConversationModel = chatConversationModel
return friendChatConversationModel
}
}
return nil
}

最佳答案

如果我理解正确的话,我认为像这样的查询会满足您的要求:

func getFriendChatConversationModel(_ friendID: String) -> ChatConversationModel? {
let realm = try! Realm()
let chatConversationModels = realm.objects(ChatConversationModel.self).filter("typeIndex = %@", ChatTypes.ChatType.oneToOne.index)
return chatConversationModels.filter("ANY userAcitivities.userID == %@", friendID).first
}

关于ios - 在 Realm Swift 中过滤嵌套数组上的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44501021/

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