caiyue1993/IceCream

BackgroundWorker may not be thread safe

jeffssss opened this issue · 2 comments

I read both BackgroundWorker and https://academy.realm.io/posts/realm-notifications-on-background-threads-with-swift/ and I found that maybe BackgroundWorker is not thread safe.

when I call BackgroundWorker.shared.start(_) simultaneously in other threadA and B (A and B save difference data), There is a certain probability that BackgroundWorker execute A or B twice.

I wonder if it's really a problem? or if it is conformance to expectation。

This certainly happens. It is not thread safe.

I added synchronized blocks when accessing block: (() -> Void)

      objc_sync_enter(self)
      self.block = block
      objc_sync_exit(self)  
    objc_sync_enter(self)
    block?()
    objc_sync_exit(self)

Here's the crash log from Crashlytics.
screenshot