gpt4 book ai didi

ios - 如果在 API 响应中被删除,则删除 Realm 对象条目

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

我得到两个用户的第一个响应,即 abc@gmail.com 和 xyz@gmail.com

[{
"email": "abc@gmail.com",
"type": "primary_email",
"linked_to": {
"_id": "DAS44564dasdDASd",
"image": null,
"company": null,
"designation": null,
"name": null
},
"active_platforms": [
"asd",
"qwe"
]
},
{
"email": "xyz@gmail.com",
"type": "primary_email",
"linked_to": {
"_id": "DAS44564dasdDASd",
"image": null,
"company": null,
"designation": null,
"name": null
},
"active_platforms": [
"asd",
"qwe"
]
}]

现在,如果我再次调用 API 时 abc@gmail.com 被删除,那么我的对象中仍然会出现 abc@gmail.com,因为它没有从我的 Realm 中删除。那么如何处理这种情况呢?

        // write request result to realm database
let entries = json["data"]
realm.beginWrite()
for (_, subJson) : (String, JSON) in entries {
let entry: AppUsers = Mapper<AppUsers>().map(JSONObject: subJson.dictionaryObject!)!
realm.add(entry, update: true)
}

do {
try realm.commitWrite()
} catch {

}

最佳答案

更新您的逻辑如下。这是其中一种方法。

向 AppUsers 模型添加一个额外的 bool 字段,比如“active”。如下更新您的代码

  // write request result to realm database
let entries = json["data"]
realm.beginWrite()

//Fetch all realm AppUsers objects
let allAppUsers = //TODO fetch all AppUsers objects here
for user in allAppUsers {
user.active = false
}

for (_, subJson) : (String, JSON) in entries {
let entry: AppUsers = Mapper<AppUsers>().map(JSONObject: subJson.dictionaryObject!)!
entry.active = true
realm.add(entry, update: true)
}

for user in allAppUsers {
if !user.active {
realm.delete(user)
}
}


do {
try realm.commitWrite()
} catch {

}

关于ios - 如果在 API 响应中被删除,则删除 Realm 对象条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43709886/

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