Ios 如何使ParseServer与ParseSwift一起工作
我有一个使用SwiftUI的iOS应用程序。 然后利用ParseSwift,我试图让它与解析服务器一起工作。由于某些原因,事情不顺利。如果有人能对这个问题有所了解,那将非常有帮助 首先,以下是服务器的代码:Ios 如何使ParseServer与ParseSwift一起工作,ios,swift,swiftui,parse-server,Ios,Swift,Swiftui,Parse Server,我有一个使用SwiftUI的iOS应用程序。 然后利用ParseSwift,我试图让它与解析服务器一起工作。由于某些原因,事情不顺利。如果有人能对这个问题有所了解,那将非常有帮助 首先,以下是服务器的代码: const express = require('express'); const ParseServer = require('parse-server').ParseServer; const path = require('path'); var mongo = require('mo
const express = require('express');
const ParseServer = require('parse-server').ParseServer;
const path = require('path');
var mongo = require('mongodb');
var MongoClient = mongo.MongoClient;
const args = process.argv || [];
const test = args.some(arg => arg.includes('jasmine'));
const databaseUri = process.env.MONGODB_URI;
if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}
const config = {
databaseURI: databaseUri,
cloud: __dirname + '/cloud/main.js',
appId: process.env.APP_ID,
masterKey: process.env.MASTER_KEY,
serverURL: process.env.SERVER_URL,
publicServerURL: process.env.PARSE_PUBLIC_SERVER_URL,
liveQuery: {
classNames: ['SampleCollection', 'MainCollection'],
},
};
const app = express();
// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
// Serve the Parse API on the /parse URL prefix
const mountPath = '/parse';
if (!test) {
const api = new ParseServer(config);
app.use(mountPath, api);
}
// Parse Server plays nicely with the rest of your web routes
app.get('/', function (req, res) {
topDisplayFunc("index", res);
});
// There will be a test page available on the /test path of your server url
// Remove this before launching your app
app.get('/test', function (req, res) {
res.sendFile(path.join(__dirname, '/public/test.html'));
});
const port = process.env.PORT || 1337;
if (!test) {
const httpServer = require('http').createServer(app);
httpServer.listen(port, function () {
console.log('parse-server-example running on port ' + port + '.');
});
// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);
}
module.exports = {
app,
config,
};
function topDisplayFunc(displayPage, response) {
MongoClient.connect(databaseUri, {useUnifiedTopology: true},
function(err, client) {
if (err) throw err;
var db = client.db();
let theCollection = db.collection('SampleCollection');
theCollection.countDocuments().then((count) => {
theCollection.insertOne({number: count + 1}, function (err) {
if(err) throw err;
db.collection('SampleCollection', function (err, collection) {
collection.find().toArray(function(err, items) {
if(err) throw err;
response.render('pages/' + displayPage, {
dataArray: items
});
client.close();
});
});
});
});
});
}
以下是iOS应用程序中的相关代码:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
.......
ParseSwift.initialize(applicationId: parseAppId,
clientKey: parseKey,
serverURL: URL(string: parseServer)!)
return true
}
然后:
// ContentView.swift
import SwiftUI
import CoreData
import ParseSwift
.......
var onlineQuery = SampleCollection.query(["number" > 5])
struct ContentView: View {
@Environment(\.managedObjectContext) var managedObjectContext
.......
var body: some View {
.......
ZStack{}.sheet(isPresented: $showingOnLine) {
OnLineView()
//.environmentObject(langType)
}
}
}
最后是视图的代码,当我在与SampleCollection相关的服务器上执行一些操作时,我希望在其中发生一些事情,但什么也没有发生
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 {
//: This is how you register to receive notifications of events related to your LiveQuery.
switch event.event {
case .entered(let object):
Text("Entered EVENT: ")
case .left(let object):
Text("Left EVENT: ")
case .created(let object):
Text("Created EVENT: ")
case .updated(let object):
Text("Updated EVENT: ")
case .deleted(let object):
Text("Deleted EVENT: ")
default:
Text("Default-event !!!!")
}
} else {
Text("Not subscribed to a query")
}
Spacer()
Text("Update DATA 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()
}
}
请您尝试使用
SampleCollection.query(“number”>5)而不是SampleCollection.query([“number”>5])
好吗?我以前有过这个,只是再试了一次。我一点区别都没有。我还尝试过SampleCollection.query()获取所有数据,但也没有成功。如果只运行一次查询(没有订阅),它会返回数据吗?我想知道您的CLP/ACL设置是否允许读取数据。您的提示可能很好,但我如何尝试?如果我注释掉包含“subscription=onlineQuery.subscripte!”的行。它似乎不起作用。您还有其他想法吗?您可以使用find()
函数记录结果。