2017-04-11 45 views
0

我是xcode,swift和領域的新手。我必須爲我的畢業設計建立一個IOS應用程序。我在處理多個客戶端請求時遇到問題。我的應用程序假設來自多個用戶的請求,我必須在服務器中處理這些請求(啓動計數器,計時器或添加,刪除,更新等),並且我的服務器正在使用領域數據庫。我的問題是如何在本地客戶端和服務器之間進行通信?我可以用swift實現服務器不是JavaScript?使用領域與迅速xcode的客戶端 - 服務器方法

回答

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需要Objective-C運行時才能工作,並且這在非Mac平臺上不可用,所以此時服務器上的Realm和Swift(除非是Mac服務器)。 Node.js是要走的路。 :)

相關問題