【问题标题】:Parse Cloud - LiveQueries - iOS Client doesn't workParse Cloud - LiveQueries - iOS 客户端不起作用
【发布时间】:2017-10-31 14:10:28
【问题描述】:

我正在尝试使用 Parse LiveQueries。 我使用这个 Parse "Bootstrap": "https://github.com/parse-community/parse-server",
我可以看到日志:info: Create new client: 1,
但我只是没有在查询中得到更新,尽管我已经订阅了它。它甚至没有到达subscription.handle 的处理程序。

config.json:

{
  "appId": "",
  "masterKey": "",
  "appName": "",
  "cloud": "./cloud/main",
  "databaseURI": "",
  "publicServerURL": "",    

  // Relevant
  "startLiveQueryServer": true,
  "liveQuery": {
    "classNames": ["Channel"]
  },
}

AppDelegate.swift:

// Initialize Parse.
let configuration = ParseClientConfiguration {
    $0.applicationId = self.PARSE_APP_ID
    $0.server = self.PARSE_SERVER
}
Parse.initialize(with: configuration)

AppDelegate.liveQueryClient = ParseLiveQuery.Client()

The Subscription Code(iOS Swift):

public static func listenSubscribedChannels(handler: @escaping (_ channel: Channel) -> Void) {
    var subscription: Subscription<PFObject>?

    let query: PFQuery<PFObject> = PFQuery(className: "Channel").whereKey("subscribers", containedIn: [PFUser.current()!.objectId])

    subscription = AppDelegate.liveQueryClient!.subscribe(query).handle(Event.updated) { _, channel in
        handler(channel)
    }
}

【问题讨论】:

    标签: ios swift parse-platform livequery parse-cloud-code


    【解决方案1】:

    此代码的问题在于您将此代码 var subscription: Subscription&lt;PFObject&gt;? 放在函数中。

    该对象必须能够保留它的内存地址才能接收事件。

    例如。

    class SomeClass {
        var objects: [PFObject] = []
        var subscription: Subscription<PFObject>?
        var subscriber: ParseLiveQuery.Client!
        let query = PFQuery(className: "Locations")
    
        func startListener() {
            // initialize the client
            subscriber = ParseLiveQuery.Client()
    
            // initialize subscriber and start subscription
            subscription = subscriber.subscribe(conversationQuery)
    
            // handle the event listenrs.
            _ = subscription?.handleEvent({ (_, event) in
                switch event {
                case .created(let object): 
                    self.objects.append(object)
                    // do stuff 
    
                default: 
                    break // do other stuff or do nothing
                }
            })
        }
    }
    

    从这段代码可以看出,我将变量放在函数定义之外,以便保留Subscription 的内存地址。

    【讨论】:

    • 更新:好的,我刚刚又试了一次,你是对的。 IDK 为什么我几天前尝试它时它不起作用,可能又犯了一个错误。非常感谢!你已经为你赢得了 50 点声望! :) 编辑:我只能在 7 小时内将赏金奖励给你
    • 不客气。文档非常缺乏,所以这是最容易被误解的东西。特别是因为 Firebase 的套接字是如何工作的,因为它的内存地址即使在函数中声明仍然存在,(这就是为什么他们使用全局处理程序)
    【解决方案2】:

    您必须在查询之前注册您的课程,例如:

    Channel.registerSubclass()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 2020-12-26
      • 2014-05-01
      • 2017-06-04
      • 2020-10-12
      相关资源
      最近更新 更多