gpt4 book ai didi

ios - 我如何通知客户端以便某个帖子不再存在?

转载 作者:行者123 更新时间:2023-11-28 06:04:34 27 4
gpt4 key购买 nike

我制作的应用程序是基于帖子/线程的。每次一个客户提交帖子时,所有其他客户都会在刷新 tableview 时收到该帖子。然后使用核心数据保存新收到的帖子。最终,对于每个刷新的客户端,都会调用函数 fetchPostsfetchPost 是一个异步函数,会返回两次回调。首先,当它从核心数据接收到帖子时,然后当服务器同步完成并收到实时数据时。

此函数的问题在于它总是会在第一个回调中返回所有帖子,包括被(其他客户端)删除的帖子。

处理该问题的正确方法是什么?这是我的代码:

static func fetchPosts(lastPost:Topic?,subject:String,complition: @escaping (_ topics:[Topic?],_ newData:Bool)->()){

var topics:[Topic?] = []

//Check Ceche. FIRST PART
do {
let fetchRequest : NSFetchRequest<DPost> = DPost.fetchRequest()
fetchRequest.fetchLimit = 20
if lastPost == nil {
fetchRequest.predicate = NSPredicate(format: "created < %@ AND subject = %@ ", NSDate(),subject)
}else{
fetchRequest.predicate = NSPredicate(format: "created < %@ AND subject = %@", argumentArray: [lastPost?.date as Any, subject])
}

let fetchedResults = try context.fetch(fetchRequest)
// _ = index
for (_, aPost) in fetchedResults.enumerated() {

topics.append(Topic(id: aPost.id!, title: aPost.title!, date: aPost.created! as Date, image: aPost.imageAddress, posterUsername: aPost.username!, posterUserid: aPost.userId!,posterImage: aPost.posterImageAddress))
//TODO: add subject id
}
}
catch {
print ("fetch task failed", error)
}
//First Callback
complition(topics,true)

//Second part
//Check server.
topics = []
var data:[String:Any] = [:]
data[K.UserInformation.sessionID] = User.currentUser!.sessionID
data[K.UserInformation.udid] = User.currentUser?.udid
if topics.last == nil {
data[K.TopicInformation.data] = "000000000000000000000000"
} else {
data[K.TopicInformation.data] = lastPost?.id
}
data[K.TopicInformation.subject] = subject
HTTPRequest.appSession.data_request(url_to_request: K.Server.domain+":8443/getPosts",method: HTTPRequest.HTTPRequestMethod.post, data: HTTPRequest.toJSON(dict: data)) { (resStr:String?) in
// return respond with information about the registrant status.
if resStr != nil{
let respond = HTTPRequest.toDict(jsonStr: resStr!)
if (respond[K.Status.success] != nil){
let postDictList = respond[K.Status.success] as! [[String:Any]]
if postDictList.count == 0 {
//Second callback
complition(topics,true)
return
}
for dict in postDictList {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

var topic:Topic? = nil
if let date = formatter.date(from: dict[K.TopicInformation.date] as! String) {
context.mergePolicy = NSOverwriteMergePolicy
let cPost = NSEntityDescription.insertNewObject(forEntityName: "CDPost", into: context) as! DPost
cPost.id = dict[K.TopicInformation.id] as? String
cPost.title = dict[K.TopicInformation.title] as? String
cPost.created = date as NSDate
cPost.imageAddress = dict[K.TopicInformation.postImageAddress] as? String
cPost.username = dict[K.TopicInformation.posterUsername] as? String
cPost.userId = dict[K.TopicInformation.posterUserid] as? String
cPost.posterImageAddress = dict[K.TopicInformation.posterImageAddress] as? String
cPost.subject = dict[K.TopicInformation.subject] as? String
do{
try context.save()
}
catch{
fatalError("Failure to save context: \(error)")
}

topic = Topic(id: dict[K.TopicInformation.id] as! String,
title: dict[K.TopicInformation.title] as! String,
date: date,
image: dict[K.TopicInformation.postImageAddress] as? String,
posterUsername: dict[K.TopicInformation.posterUsername] as! String,
posterUserid: dict[K.TopicInformation.posterUserid] as! String, posterImage: dict[K.TopicInformation.posterImageAddress] as? String)
}
topics.append(topic!)
}
complition(topics,true)
return
}

if(respond[K.Status.error] != nil){
print(respond["errmsg"] as! String)
}
}

服务器端是用 NodeJS 编写的 Mongodb 是我正在使用的数据库。让我知道它是否相关,以便可以编辑掉/插入一些标签。

最佳答案

如果你有获取限制,我认为你不能通过比较获取的帖子和存储在你的 CoreData 中的帖子来在本地完成,最好是在帖子中添加一个 unread 标签并更新它您的 API,当 fetch 可以使用 unread 标签获取已删除和正常的帖子时,另一个想法是使用上次登录时间并从该时间获取所有帖子,包括已删除的帖子

关于ios - 我如何通知客户端以便某个帖子不再存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48615104/

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