When are Realm notifications sent to the main stream after recording in the background thread?

I see failures that either should not be possible or very possible, and the documentation is just not clear enough for what.

UPDATE:

Although I disagree with the comment below, asking me to split this up into a few SO questions, if anyone can focus on this, I think it helped a lot:

When are notifications sent to the main thread? Is it possible that the results for the main thread are different from the results of the previous runloop without notice of the difference?

If the answer to this question is yes, the results may differ from the previous runloop without notice, I would say that it is CRUCIAL to get this in the documentation somewhere.

Background Records

At first, I think it’s important to experience what I’m already doing for the record. All my records are done using a method that essentially looks like this (error handling):

func write(block: @escaping (Realm) -> ()) {
    somePrivateBackgroundSerialQueue.async {
        autoreleasepool {
            let realm = try! Realm()
            realm.refresh()
            try? realm.write { block(realm) }
        }
    }
}

Nothing crazy here, pretty well documented at your end.

Notifications and table views

The main question I have is when are notifications sent to the main stream after recording from the background stream? I have a complex tabular view (several sections, declarations every 5th row) supported by the results of the area. My main data source is as follows:

enum StoryRow {
    case story(Story) // Story is a RealmSwift.Object subclass
    case ad(Int)
}

class StorySection {
    let stories: Results<Story>

    var numberOfRows: Int {
        let count = stories.count
        return count + numberOfAds(before: count)
    }

    func row(at index: Int) -> StoryRow {
        if isAdRow(at: index) {
            return .ad(index)
        } else {
            let storyIndex = index - numberOfAds(before: index)
            return .story(stories[storyIndex])
        }
    }
}

var sections: [StorySection]

... sections[indexPath.section].row(at: indexPath.row) ...

, , , , results.filter(...date query...) . , results.observe(...) ( , ) . , , - , , .

, , , tableView.reloadData() - :

guard tableView.indexPathsForVisibleRows?.contains(indexPath) == true else { return }
tableView.beginUpdates()
tableView.reloadRows(at: [indexPath], with: .automatic)
tableView.endUpdates()

, , , .

  • - ?
  • , reloadData(), - ?
  • - , ?
+4
1

, , , . , , Realm, , , indexPath , , , .


, Realm - : , , . , . :

, ( refresh() , ). , .

, , , , , -, tableView.reloadData() . , , , , .

+2

Source: https://habr.com/ru/post/1688985/


All Articles