我如何通知客户端以便某个 post 不再存在?

How can I notify client side so that a certain post does not exist anymore?

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

这个函数的问题在于它总是会在第一个回调中return所有posts,包括被删除的那个(由其他客户)。

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

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是我用的数据库。让我知道它是否相关,以便可以编辑 out/in 一些标签。

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