我是xcode,swift和realm的新手。我必须为我的毕业设计建立一个IOS应用程序。我有一个如何处理多个客户端请求的问题。我的应用程序假设从多个用户获取请求,我必须在服务器中处理这些请求(启动计数器,计时器,或添加,删除,更新等),我的服务器正在使用领域数据库。我的问题是如何在本地客户端和服务器之间进行通信?我可以使用swift而不是javascript实现服务器吗?
答案 0 :(得分:0)
如果您正在使用Realm Mobile Platform进行客户端与服务器的交互,则应该能够使用Realm Object Server的事件处理功能来检测和响应用户触发的请求。您可以download a trial of the Professional Edition(这应该足以满足您作为私人项目的需求。)
注册事件处理程序的代码如下所示(取自the Realm docs页面)
var Realm = require('realm');
// Insert the Realm admin token here
// Linux: cat /etc/realm/admin_token.base64
// macOS: cat realm-object-server/admin_token.base64
var ADMIN_TOKEN = 'ADMIN_TOKEN';
// the URL to the Realm Object Server
var SERVER_URL = 'realm://127.0.0.1:9080';
// The regular expression you provide restricts the observed Realm files to only the subset you
// are actually interested in. This is done in a separate step to avoid the cost
// of computing the fine-grained change set if it's not necessary.
var NOTIFIER_PATH = '/^\/([0-9a-f]+)\/private$/';
// The handleChange callback is called for every observed Realm file whenever it
// has changes. It is called with a change event which contains the path, the Realm,
// a version of the Realm from before the change, and indexes indication all objects
// which were added, deleted, or modified in this change
function handleChange(changeEvent) {
// Extract the user ID from the virtual path, assuming that we're using
// a filter which only subscribes us to updates of user-scoped Realms.
var matches = changeEvent.path.match(/^\/([0-9a-f]+)\/private$/);
var userId = matches[1];
var realm = changeEvent.realm;
var coupons = realm.objects('Coupon');
var couponIndexes = changeEvent.changes.Coupon.insertions;
for (var couponIndex in couponIndexes) {
var coupon = coupons[couponIndex];
if (coupon.isValid !== undefined) {
var isValid = verifyCouponForUser(coupon, userId);
// Attention: Writes here will trigger a subsequent notification.
// Take care that this doesn't cause infinite changes!
realm.write(function() {
coupon.isValid = isValid;
});
}
}
}
// create the admin user
var adminUser = Realm.Sync.User.adminUser(adminToken);
// register the event handler callback
Realm.Sync.addListener(SERVER_URL, adminUser, NOTIFIER_PATH, 'change', handleChange);
console.log('Listening for Realm changes');
不幸的是,此时服务器上的Realm和Swift都不支持(除非它是Mac服务器),因为Realm Swift需要Objective-C运行时才能工作,而且不是这样。在非Mac平台上可用。 Node.js是要走的路。 :)