にバックグラウンドスレッドを使用している配列を挿入します。 残りの応答を受け取るとすぐにコールバック関数を呼び出し、配列をバックグラウンドスレッドのdbに挿入します。 私は私があるため、オブジェクトの更にによるキャッチされないにアプリを終了迅速分野私はオブジェクトの配列が、私は<strong>背景</strong>スレッドでレルムDBに挿入し、<strong>メイン</strong>スレッドでuicollectionviewで使用する残りの応答を介して受信していメイン
を挿入していないと仮定した例外(が下記参照)を取得していた背景に挿入されるオブジェクトのメインスレッドのプロパティにアクセスしようとしています問題例外 'RLMException'、理由: '不正なスレッドからアクセスされた領域。
モデル
バックグラウンドスレッドで挿入class User : Object, Mappable {
dynamic var firstName: String?
dynamic var lastName: String?
required convenience init?(map: Map){
self.init()
}
func mapping(map: Map) {
firstName <- map["firstName"]
lastName <- map["lastName"]
}
}
...
UIでのレンダリングDispatchQueue.global().async {
let realm = try! Realm()
try! realm.write {
realm.add(users)
}
}
...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! UserViewCell
let user = users[indexPath.row]
cell.firstName.text = user.firstName
cell.lastName.text = user.lastName
}
その例外は、どちらかの発生に注意してくださいfirstNameまたはlastNameにアクセスします。
、私は
notificationTokenの使用を検討しましたか? – EpicPandaForce
それは私が考えていた唯一のことですが、それが良い考えであるかどうかは分かりませんでした – mihatel
これはRealmの通知トークンのためのものです – EpicPandaForce