尝试使用 ParseSwift

问题描述

在使用 SwiftUI 并与解析服务器通信的 iOS 应用程序中,我尝试使用 ParseSwift,遵循 here 找到的信息。

但我什么也没看到(即使我在服务器上做了一些更改),除了一开始的显示切换:

"Not subscribed to a query"

到:

"Subscribed to query!"

换句话说,我不确定什么有效,什么无效。

这是相关视图的代码

import SwiftUI
import ParseSwift

struct OnLineView: View {
    @Observedobject var subscription = onlineQuery.subscribe!
    
    var body: some View {
        vstack {
            if subscription.subscribed != nil {
                Text("Subscribed to query!")
            } else if subscription.unsubscribed != nil {
                Text("Unsubscribed from query!")
            } else if let event = subscription.event {
                // THE EXECUTION FLOW NEVER REACHES THIS POINT.

                //: This is how you register to receive notifications of events related to your LiveQuery.
                switch event.event {

                case .entered(let object):
                    Text("Entered with TITLE: \(object.TITLE!)")
                case .left(let object):
                    Text("Left with TITLE: \(object.TITLE!)")
                case .created(let object):
                    Text("Created with TITLE: \(object.TITLE!)")
                case .updated(let object):
                    Text("Updated with TITLE: \(object.TITLE!)")
                case .deleted(let object):
                    Text("Deleted with TITLE: \(object.TITLE!)")
                default:
                    Text("Default-event !!!!")
                }
            } else {
                Text("Not subscribed to a query")
            }

            Spacer()

            Text("Update MyClassUnit in Parse Dashboard to see changes here")

            Button(action: {
                try? onlineQuery.unsubscribe()
            },label: {
                Text("Unsubscribe")
                    .font(.headline)
                    .background(Color.red)
                    .foregroundColor(.white)
                    .padding()
                    .cornerRadius(20.0)
                    .frame(width: 300,height: 50)
            })
        }
    }
}

struct OnLineView_Previews: PreviewProvider {
    static var previews: some View {
        OnLineView()
    }
}

此外,这是相关代码的侧面部分:

//: Create your own value typed ParSEObject.
struct MyClassUnit: ParSEObject {
    //: These are required for any Object.
    var objectId: String?
    var createdAt: Date?
    var updatedAt: Date?
    var ACL: ParseACL?

    //: Your own properties.
    var TITLE: String?

    //: Custom initializer.
    init(title: String) {
        self.TITLE = title
    }
}

var onlineQuery = MyClassUnit.query()

虽然我对 SwiftUI 和 Parse-Server 有一些经验,但这是我第一次使用 ParseSwift;所以我很可能漏掉了一些基本点。

最后,我希望能够在我的视图中捕捉到一些关于我的服务器数据库上的信息。

我希望有 ParseSwift 经验的人能够指出我所做的一些问题,或者给我一个提示,告诉我从哪里可以看到以便取得一些进展。

.... 在这一点上,我还没有触及我的解析服务器。 我在文档中看到我需要设置 LiveQuery 服务器。但是进一步阅读并没有说明我必须做什么。

这是否意味着我必须对解析服务器的设置进行一些修改?或者是关于不同的东西? 实际上,我不知道该怎么办。所以这可能是问题所在。

服务器端的这篇文章也可能有用:

var api = new ParseServer({
  databaseURI: databaseUri,cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',appId: process.env.APP_ID,masterKey: process.env.MASTER_KEY,filesAdapter: "@parse/s3-files-adapter",serverURL: process.env.SERVER_URL,publicServerURL: process.env.PARSE_PUBLIC_SERVER_URL,appName: 'TheApp',verifyUserEmails: true,emailAdapter: {
    module: '@parse/simple-mailgun-adapter',options: {
      fromAddress: 'special@rmail.com',domain: 'xtkd.net',apiKey: process.env.EML_ADAPTER_KEY,}
  },liveQuery: {
    classNames: ['CollectionMain','CollectionSecond']
  }
});

另一条相关信息。这是我在日志中看到的:

... app[web.1]: (node:23) Warning: Accessing non-existent property 'count' of module exports inside circular dependency
... app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
... app[web.1]: (node:23) Warning: Accessing non-existent property 'findOne' of module exports inside circular dependency
... app[web.1]: (node:23) Warning: Accessing non-existent property 'remove' of module exports inside circular dependency
... app[web.1]: (node:23) Warning: Accessing non-existent property 'updateOne' of module exports inside circular dependency
... app[web.1]: parse-server-example running on port 27596.
... app[web.1]: info: Parse LiveQuery Server starts running
... heroku[web.1]: State changed from starting to up

这是我正在使用的 package.json:

{
    .....
    "dependencies": {
      "@parse/s3-files-adapter": "^1.4.0","@parse/simple-mailgun-adapter": "^1.0.3","aws-sdk": "^2.637.0","bcryptjs": "^2.4.3","body-parser": "1.19.0","ejs": "^3.0.1","express": "^4.17.1","parse": "~1.8.0","parse-server": "^3.9.0"
    },"scripts": {
      "start": "node index.js"
    },"engines": {
      "node": ">=4.3"
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)