Skip to content

Combine  #290

@Kharchevskyi

Description

@Kharchevskyi

Consider usage of Combine framework for handling async operations
https://developer.apple.com/documentation/combine

Pros:

  • Native framework
  • Declarative Swift API
  • Easier to onboard new developers. Native and fast growing solution.
  • Less number of dependencies in app
  • Bunch of native operators(retry, map, filter and so on)
  • Easier to switch between threads. So for example we can fetch folders on background thread and in easy way switch to main to save them to DB. More control over thread switching
  • Easy retry for operation with just .retry(2)
private func getAndSaveFolders() -> Promise<[FolderViewModel]> {
        Promise<[FolderViewModel]> { [weak self] resolve, _ in
            guard let self = self else { throw AppErr.nilSelf }
            // fetch all folders
            let remoteFolders = try await(self.remoteFoldersProvider.fetchFolders())

            DispatchQueue.main.async {
                // save to Realm
                let folders = remoteFolders.compactMap(FolderObject.init)
                self.localFoldersProvider.save(folders: folders)

                // save trash folder path
                self.saveTrashFolderPath(with: folders)

                // return folders
                resolve(folders.map(FolderViewModel.init))
            }
      }
 }

vs (example)

fetchFolders
   .retry(2) 
   .map(FolderObject.init)
   .receive(on: .main)
   .save()
   .map(FolderViewModel.init)
  • More control over threads
func fetch() -> Promise<[FolderViewModel]> {
        Promise(on: .main) { (_, _) in
            
        }
}

In case fetch() will be executed in another Promise with await(fetch()) this will cause to UI freeze

  • Easier switch to SwiftUI(in some point in future)

Cons:

  • Available from iOS 13

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions