小编典典

解析云-LiveQueries-iOS客户端不起作用

swift

我正在尝试使用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)
    }
}

阅读 277

收藏
2020-07-07

共1个答案

小编典典

这段代码的问题是您正在将该代码var subscription: Subscription<PFObject>?放置在函数中。

该对象必须能够保留其内存地址,以便接收事件。

例如。

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保留的内存地址。

2020-07-07