考虑简单的模型:
public class Session : RealmObject
{
[ObjectId]
public string UserId { get; set; }
public string Token { get; set; }
}
如何Session
通过 ID 获取实例或者null
如果它不存在?
var realm = Realm.GetInstance ();
var q = realm.All<Session> ().Where ((x) => x.UserId = "1");
// This won't work if no session is saved:
// var session = q.First ();
// and this won't either
// var session = q.FirstOrDefault ();
// And this is mmm... kind of strange but it's working :)
var session = q.Count() > 0 ? q.First() : null;
那么,它应该如何通过设计来完成呢?