回答
任何答案不幸的是,我不實際使用的iOS版本,但我確實使用了Android版本,這一天是一天其功能集與iOS版本越來越相似,並且它們共享相同的core
,並且它們更接近於通過object-store
提供相同的統一行爲。
這個答案的大部分將基於Swift API文檔。 (境界夫特 2.6.1)
境界是默認的對象存儲。從技術上講它存儲你的數據在一個模式,其中該模式是由類定義,就像
// from https://realm.io/docs/swift/latest/#models
class Person: Object {
dynamic var name = ""
dynamic var birthdate = NSDate(timeIntervalSince1970: 1)
let dogs = List<Dog>()
}
現在什麼是關於境界有趣的是,它不是一個關係型數據庫;它直接存儲對象。實際上,由Realm管理的對象(也可以通過對Realm的查詢獲得,或者由Realm創建的新創建的對象)直接映射到底層的Realm文件,並且不會將數據複製到字段,訪問器可以直接讀取和寫入Realm文件。
這種「直接訪問」的結果是所有的數據只有在訪問時才加載(懶惰評估),因此不需要高速緩存被管理對象(!)。
所有寫入都是事務性的。在事務之外,被管理的RealmObjects不能被修改。
你的對象之間,你可以有關係(鏈接):具有其相應的backlink
,它可以定義爲「
// from https://realm.io/docs/swift/latest/#relationships
class Dog: Object {
// ... other property declarations
dynamic var owner: Person? // to-one relationships must be optional
}
class Person: Object {
// ... other property declarations
let dogs = List<Dog>() // to-many relationship
}
任何關係(一對一,一對多)鏈接到這個對象的對象「。
// from https://realm.io/docs/swift/latest/#relationships
class Dog: Object {
dynamic var name = ""
dynamic var age = 0
let owners = LinkingObjects(fromType: Person.self, property: "dogs")
}
領域的管理對象是「活的,數據的不可變的意見」(從here),這發生變異的地方,您會收到變更通知它通過notification token
(從here)。同樣適用於任何管理的RealmObject,但也查詢結果。
意思是,查詢結果會自動異步評估,並且只有在給定索引處訪問的元素纔會從數據庫中以延遲加載的方式讀取!因此,分頁是不需要的。
任何線程上的任何寫操作都會自動向與運行循環相關聯的線程發送通知,並且會自動更新查詢結果,並調用更改偵聽器(通知塊)。
// from https://realm.io/docs/swift/latest/#collection-notifications
override func viewDidLoad() {
super.viewDidLoad()
let realm = try! Realm()
let results = realm.objects(Person.self).filter("age > 5")
// Observe Results Notifications
notificationToken = results.addNotificationBlock { [weak self] (changes: RealmCollectionChange) in
guard let tableView = self?.tableView else { return }
switch changes {
case .initial:
// Results are now populated and can be accessed without blocking the UI
tableView.reloadData()
break
case .update(_, let deletions, let insertions, let modifications):
// Query results have changed, so apply them to the UITableView
tableView.beginUpdates()
tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
with: .automatic)
tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
tableView.endUpdates()
break
case .error(let error):
// An error occurred while opening the Realm file on the background worker thread
fatalError("\(error)")
break
}
}
}
deinit {
notificationToken?.stop()
}
最知名的限制是RealmObjects,RealmResults和境界不能線程之間傳遞。 (其他限制是here)。
給定線程上的RealmObject/RealmResults/Realm只能在打開相應Realm實例的線程上訪問(請參閱here)。 (例外是在線程之間使用ThreadSafeReference
發送的RealmObjects,請參閱here)。
因此,後臺線程需要自己的領域實例,一般包裹在一個autoreleasepool
,看到here。
// from https://realm.io/docs/swift/latest/#using-a-realm-across-threads
DispatchQueue(label: "background").async {
autoreleasepool {
// Get realm and table instances for this thread
let realm = try! Realm()
// Break up the writing blocks into smaller portions
// by starting a new transaction
for idx1 in 0..<1000 {
realm.beginWrite()
// Add row via dictionary. Property order is ignored.
for idx2 in 0..<1000 {
realm.create(Person.self, value: [
"name": "\(idx1)",
"birthdate": Date(timeIntervalSince1970: TimeInterval(idx2))
])
}
// Commit the write transaction
// to make this data available to other threads
try! realm.commitWrite()
}
}
}
謝謝你的回答,但是我對數據庫的內部機制和使用案例更感興趣 – ilyailya
我認爲這就是你在'設計Realm數據庫時是否有細微差別'的意思?'但是我想你可以查看REALM THREADING DEEP DIVE – EpicPandaForce
https://news.realm.io/news/threading-deep-dive/ – EpicPandaForce
- 1. 什麼是領域?
- 2. 什麼是領域?
- 3. 什麼是數據庫理論領域的「完全約束」?
- 4. 什麼是htdigest領域?
- 5. 爲什麼我的領域數據庫如此之大?
- 6. 在數據庫領域OK
- 7. 加密領域數據庫
- 8. 領域數據庫性能
- 9. 更新領域數據庫
- 10. 重建領域數據庫
- 11. 導入數據到領域數據庫
- 12. 如何刪除領域數據庫中的領域列表
- 13. 未知領域的數據庫設計
- 14. 從SQLite數據庫檢索多個領域,這些領域
- 15. 領域數據庫問題。 - 無默認領域配置是發現 -
- 16. 什麼是科特林支持領域?
- 17. 你在IT領域的領導風格是什麼?
- 18. 加密領域數據庫,並崩潰
- 19. 數據庫領域Reports XI中
- 20. 枚舉存儲在數據庫領域
- 21. 數據庫表/領域應用程式
- 22. Android領域數據庫安全
- 23. 相關領域無法從數據庫
- 24. 領域預填充數據庫
- 25. 領域數據庫文件大小
- 26. 領域數據庫沒有更新
- 27. 什麼是領先的LINQ for JavaScript庫?
- 28. 檢查是否領域是數據庫填充
- 29. 使用什麼樣的數據庫?
- 30. IronPython - 什麼樣的數據庫可用
看到這個https://realm.io/news/realm-object-centric-present-day-database-mobile-applications/ –
這是一個對象數據庫。 –