Swift Combine properties inheritance throws 'Fatal error: Call of deleted method' on Xcode 11.4 beta 2 / iOS 13.4

主宰稳场 提交于 2020-05-11 06:42:36

问题


I'm trying to using Swift Combine to get the changed event of a property.

I have this class that publish the isLogged property

class CurrentUser: Account {
    static let me = CurrentUser() //Singleton 

    @Published var isLogged: Bool = false

}

that inherit from this other class that publish the profileImageVersion property

class Account {

    @Published var profileImageVersion: String?

    init(){
       self.profileImageVersion = ""
    }
}

I'm trying to subscribe to the published inherit profileImageVersion property like this without success!

// Subscribe to account image changes
userImageChangedSubscriber = CurrentUser.me.$profileImageVersion.receive(on: DispatchQueue.main).sink(receiveValue: { (imageVersion) in           
       ...
    }
})

The error is Fatal error: Call of deleted method

if, on the other hand, I subscribe to the isLogged property, all Is working fine...

// Subscribe to logged changes
userLoggedSubscriber = CurrentUser.me.$isLogged.receive(on: DispatchQueue.main).sink(receiveValue: { (logged) in
   ...
})

This error is thrown only on Xcode 11.4 beta 2 / iOS 13.4.
Using Xcode 11.3.1 / 13.3 all is working fine!


回答1:


I have this same crash, and as a temporary workaround I found that moving all your published properties to the concrete class you are using will fix the issue. I had a setup like this:

class EpisodesViewModel {
  @Published var episodes: [Episode]

  init(fetchRequest: NSFetchRequest<Episode>, context: NSManagedObjectContext? = nil) throws {
    ...
  }
  ...
}

With a subclass of this model that simply gave a fetch request:

final class LatestEpisodesViewModel: EpisodesViewModel {
    init() throws {
        try super.init(fetchRequest: Episode.latestFetchRequest())
    }
}

By changing my setup to this I was able to fix the crash:

class EpisodesViewModel {
    var fetchedEpisodes: [Episode]

    init(fetchRequest: NSFetchRequest<Episode>, context: NSManagedObjectContext? = nil) throws {
        ...
    }
    ...
}

final class LatestEpisodesViewModel: EpisodesViewModel {

    @Published var episodes: [Episode] = []

    override var fetchedEpisodes: [Episode] {
        didSet {
            episodes = fetchedEpisodes
        }
    }

    init() throws {
        try super.init(fetchRequest: Episode.latestFetchRequest())
    }
}

This certainly seems like an Apple bug to me, but this got me around the issue in the meantime.




回答2:


I have this same crash on Xcode 11.4.1. I use "Clean Build Folder", build my project again and all works now fine !



来源:https://stackoverflow.com/questions/60464226/swift-combine-properties-inheritance-throws-fatal-error-call-of-deleted-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!