Skip to content

Commit

Permalink
- Replacing DispatchQueue.main.async* with OnMainThread and a newly a…
Browse files Browse the repository at this point in the history
…dded OnMainThread(after: timeInterval)
  • Loading branch information
felix-schwarz committed Jan 18, 2019
1 parent d00a7bc commit 7ecec80
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion ownCloud/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
OnMainThread(after: 2.0) {
completionHandler(.newData)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ extension ConnectionIssueViewController {
if let issue = displayIssues?.displayIssues[indexPath.row], issue.type == OCIssueType.certificate {
OCCertificateDetailsViewNode.certificateDetailsViewNodes(for: issue.certificate, withValidationCompletionHandler: { (certificateNodes) in
let certDetails: NSAttributedString = OCCertificateDetailsViewNode .attributedString(withCertificateDetails: certificateNodes)
DispatchQueue.main.async {

OnMainThread {
let issuesVC = CertificateViewController(localizedDescription: certDetails)
issuesVC.modalPresentationStyle = .overCurrentContext
self.present(issuesVC, animated: true, completion: nil)
Expand Down
6 changes: 3 additions & 3 deletions ownCloud/Client/ClientQueryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ class ClientQueryViewController: UITableViewController, Themeable {

switch query.state {
case .idle:
DispatchQueue.main.async {
OnMainThread{
if !self.refreshController!.isRefreshing {
self.refreshController?.beginRefreshing()
}
}

case .contentsFromCache, .stopped:
DispatchQueue.main.async {
OnMainThread {
self.tableView.refreshControl = nil
}

Expand Down Expand Up @@ -608,7 +608,7 @@ extension ClientQueryViewController : OCQueryDelegate {

func queryHasChangesAvailable(_ query: OCQuery) {
query.requestChangeSet(withFlags: OCQueryChangeSetRequestFlag(rawValue: 0)) { (query, changeSet) in
DispatchQueue.main.async {
OnMainThread {

switch query.state {
case .idle, .targetRemoved, .contentsFromCache, .stopped:
Expand Down
6 changes: 3 additions & 3 deletions ownCloud/Client/ClientRootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ class ClientRootViewController: UITabBarController {
openProgress.totalUnitCount = 1

// Start showing connection status with a delay of 1 second, so "Offline" doesn't flash briefly
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0, execute: { [weak self] () in
OnMainThread(after: 1.0) { [weak self] () in
self?.connectionStatusObservation = core?.observe(\OCCore.connectionStatus, options: [.initial], changeHandler: { [weak self] (_, _) in
self?.updateConnectionStatusSummary()
})
})
}

self.progressSummarizer?.stopTracking(progress: openProgress)
})
Expand Down Expand Up @@ -173,7 +173,7 @@ class ClientRootViewController: UITabBarController {
}

func coreReady() {
DispatchQueue.main.async {
OnMainThread {
let queryViewController = ClientQueryViewController(core: self.core!, query: OCQuery(forPath: "/"))

queryViewController.navigationItem.leftBarButtonItem = self.logoutBarButtonItem()
Expand Down
4 changes: 2 additions & 2 deletions ownCloud/Server List/ServerListTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class ServerListTableViewController: UITableViewController, Themeable {
var ignoreServerListChanges : Bool = false

@objc func serverListChanged() {
DispatchQueue.main.async {
OnMainThread {
if !self.ignoreServerListChanges {
self.tableView.reloadData()
}
Expand Down Expand Up @@ -315,7 +315,7 @@ class ServerListTableViewController: UITableViewController, Themeable {
let vault : OCVault = OCVault(bookmark: bookmark)

vault.erase(completionHandler: { (_, error) in
DispatchQueue.main.async {
OnMainThread {
if error != nil {
// Inform user if vault couldn't be erased
let alertController = UIAlertController(title: NSString(format: "Deletion of '%@' failed".localized as NSString, bookmark.shortName as NSString) as String,
Expand Down
8 changes: 4 additions & 4 deletions ownCloud/Settings/Passcode/AppLockManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,23 +299,23 @@ class AppLockManager: NSObject {
context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { (success, error) in
if success {
//Fill the passcode dots
DispatchQueue.main.async {
OnMainThread {
self.passcodeViewController?.passcode = self.passcode
}
//Remove the passcode after small delay to give user feedback after use the biometrical unlock
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
OnMainThread(after: 0.3) {
self.attemptUnlock(with: self.passcode)
}
} else {
if let error = error {
switch error {
case LAError.biometryLockout:
DispatchQueue.main.async {
OnMainThread {
self.passcodeViewController?.errorMessage = error.localizedDescription
}

case LAError.authenticationFailed:
DispatchQueue.main.async {
OnMainThread {
self.attemptUnlock(with: nil, customErrorMessage: "Biometric authentication failed".localized)
}

Expand Down
2 changes: 1 addition & 1 deletion ownCloud/Settings/Passcode/PasscodeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class PasscodeViewController: UIViewController, Themeable {
if let passcode = passcode {
if passcode.count == passcodeLength {
// Delay to give feedback to user after the last digit was added
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
OnMainThread(after: 0.1) {
self.completionHandler?(self, passcode)
}
}
Expand Down
2 changes: 1 addition & 1 deletion ownCloud/Theming/Resources/ThemeResource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ThemeResource : NSObject {
DispatchQueue.global(qos: .userInteractive).async {
let requestedResource : Any? = self.resource(for: theme)

DispatchQueue.main.async {
OnMainThread {
completion(requestedResource)
}
}
Expand Down
14 changes: 9 additions & 5 deletions ownCloud/Tools/DispatchQueueTools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
*
*/

import UIKit
import Foundation

func OnMainThread(async: Bool = true, _ block: @escaping () -> Void) {
if async {
DispatchQueue.main.async(execute: block)
func OnMainThread(async: Bool = true, after: TimeInterval? = nil, _ block: @escaping () -> Void) {
if let after = after {
DispatchQueue.main.asyncAfter(deadline: .now() + after, execute: block)
} else {
DispatchQueue.main.sync(execute: block)
if async {
DispatchQueue.main.async(execute: block)
} else {
DispatchQueue.main.sync(execute: block)
}
}
}
6 changes: 3 additions & 3 deletions ownCloud/UI Elements/Progress/CollapsibleProgressBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class CollapsibleProgressBar: UIView, Themeable {

// MARK: - Collapsing
func collapse(_ collapse: Bool, animate: Bool) {
DispatchQueue.main.async {
OnMainThread {
if self.isCollapsed != collapse {
self.isCollapsed = collapse

Expand Down Expand Up @@ -186,7 +186,7 @@ class CollapsibleProgressBar: UIView, Themeable {
delay = 0.5
}

DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
OnMainThread(after: delay) {
self.evaluateAutoCollapse(shouldCollapse)
}
}
Expand Down Expand Up @@ -236,7 +236,7 @@ class CollapsibleProgressBar: UIView, Themeable {
}

if doDispatch {
DispatchQueue.main.async {
OnMainThread {
self.performUpdate()
}
}
Expand Down
4 changes: 2 additions & 2 deletions ownCloud/UI Elements/Progress/ProgressSummarizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ class ProgressSummarizer: NSObject {

if scheduleUpdate {
if minimumUpdateTimeInterval > 0 {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + minimumUpdateTimeInterval) {
OnMainThread(after: minimumUpdateTimeInterval) {
self.performNeededUpdate()
}
} else {
DispatchQueue.main.async {
OnMainThread {
self.performNeededUpdate()
}
}
Expand Down
2 changes: 1 addition & 1 deletion ownCloudTests/UtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class UtilsTests {
let vault : OCVault = OCVault(bookmark: bookmark)

vault.erase(completionHandler: { (_, error) in
DispatchQueue.main.async {
OnMainThread {
if error == nil {
OCBookmarkManager.shared.removeBookmark(bookmark)
} else {
Expand Down

0 comments on commit 7ecec80

Please sign in to comment.