所以,这是我的问题:
iOS 上领域的本地路径位于 Documents Directory 中。我可以打开它们:
let realm = try! Realm()
打开同步领域是不同的,因为它们通过 URL https://realm.io/docs/swift/latest/#realms定位
我有一个 UICollectionView,Results<Object>
我可以通过在启动时写入领域来渲染默认数据以从单独的文件中调用 AppDelegate
单独的文件
class SetUpData {
// MARK: - Seed Realm
static func defaults() {
let realm = try! Realm()
guard realm.isEmpty else { return }
try! realm.write {
realm.add(List.self())
}
}
}
应用代理
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// MARK: - Set Up Realm If Deleted
var config = Realm.Configuration()
config.deleteRealmIfMigrationNeeded = true
Realm.Configuration.defaultConfiguration = config
SetUpData.defaults()
return true
}
从这里,客户端(iOS),我能够成功登录(滚动我自己的登录,但值对应于领域对象服务器(ROS)管理员用户)并从中检索默认值List.cell
并开始编写“列表”到我的应用程序。
但是,当我配置我的领域时,打开一个Sync Configuration
同步领域需要一个已通过对象服务器身份验证并有权打开该领域的用户,我合理地崩溃了我的cellForItemAtIndexPath
return lists.count
致命错误:在展开可选值时意外发现 nil,因为有是没有要返回的初始数据。
那讲得通。但是我该怎么办?
我需要在默认配置中创建一个 Realm 文件并将其迁移到服务器吗?我尝试使用下面的代码将我的配置更改为 App Delegate 中的 Sync 对象(这是我在 中使用的ListViewController
)。没有骰子。
private func setUpRealm() {
let username = "\(LoginViewController().username.text!)"
let password = "\(LoginViewController().password.text!)"
SyncUser.logIn(with: SyncCredentials.usernamePassword(username: username, password: password, register: true), server: URL(string: "http://000.000.000.000:9080")!) { (user, error) in
guard let user = user else {
fatalError(String(describing: error))
}
DispatchQueue.main.async {
let configuration = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: URL(string: "realm://000.000.000.000:9080/~/realmList")!))
let realm = try! Realm(configuration: configuration)
self.lists = realm.objects(List.self).sorted(byKeyPath: "created", ascending: false)
self.notificationToken = self.lists.addNotificationBlock { [weak self] (changes: RealmCollectionChange) in
guard (self?.collectionView) != nil else { return }
switch changes {
case .initial:
self?.collectionView.reloadData()
break
case .update(_, let deletions, let insertions, let modifications):
self?.collectionView.performBatchUpdates({
self?.collectionView.insertItems(at: insertions.map({ IndexPath(row: $0, section: 0)}))
self?.collectionView.deleteItems(at: deletions.map({ IndexPath(row: $0, section: 0)}))
self?.collectionView.reloadItems(at: modifications.map({ IndexPath(row: $0, section: 0)}))
}, completion: nil)
break
case .error(let error):
print(error.localizedDescription)
break
}
}
}
}
}