2017-02-20 242 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