2017-02-20 239 views
1

什麼樣的數據庫是領域?它是ORM?還是像對象數據庫一樣工作?也許數據庫結構會影響設計過程?設計Realm數據庫有沒有細微之處?什麼樣的數據庫是領域?

我問在這裏,因爲我還沒有發現在官方網站

+0

看到這個https://realm.io/news/realm-object-centric-present-day-database-mobile-applications/ –

+0

這是一個對象數據庫。 –

回答

3

任何答案不幸的是,我不實際使用的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() 
    } 
    } 
} 
+0

謝謝你的回答,但是我對數據庫的內部機制和使用案例更感興趣 – ilyailya

+0

我認爲這就是你在'設計Realm數據庫時是否有細微差別'的意思?'但是我想你可以查看REALM THREADING DEEP DIVE – EpicPandaForce

+0

https://news.realm.io/news/threading-deep-dive/ – EpicPandaForce