【发布时间】:2017-09-07 00:39:06
【问题描述】:
所以,这是我的问题:
iOS 上领域的本地路径位于文档目录中。我可以打开它们:
let realm = try! Realm()
打开同步领域是不同的,因为它们是通过 URL 定位的 https://realm.io/docs/swift/latest/#realms
我有一个带有Results<Object> 的 UICollectionView 我可以通过在启动时写入领域来呈现默认数据以从单独的文件中调用 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
}
}
}
}
}
【问题讨论】:
标签: ios swift uicollectionview realm realm-object-server