Skip to content

Commit 589d76c

Browse files
Merge pull request #79 from Adamant-im/dev/fixed_nodes_crash
Fixed node test crash
2 parents f44ea44 + 1b45121 commit 589d76c

18 files changed

+131
-245
lines changed

Adamant.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
3AA2D5F7280EADE3000ED971 /* SocketService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA2D5F6280EADE3000ED971 /* SocketService.swift */; };
1616
3AA2D5FA280EAF5D000ED971 /* AdamantSocketService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA2D5F9280EAF5D000ED971 /* AdamantSocketService.swift */; };
1717
3C06931576393125C61FB8F6 /* Pods_Adamant.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 33975C0D891698AA7E74EBCC /* Pods_Adamant.framework */; };
18+
553B1284281C6EE100FFF24C /* GCDUtilites.swift in Sources */ = {isa = PBXBuildFile; fileRef = 553B1283281C6EE100FFF24C /* GCDUtilites.swift */; };
1819
6403F5DB2272389800D58779 /* (null) in Sources */ = {isa = PBXBuildFile; };
1920
6403F5DE22723C6800D58779 /* DashMainnet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6403F5DD22723C6800D58779 /* DashMainnet.swift */; };
2021
6403F5E022723F6400D58779 /* DashWalletRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6403F5DF22723F6400D58779 /* DashWalletRouter.swift */; };
@@ -612,6 +613,7 @@
612613
3AA2D5F6280EADE3000ED971 /* SocketService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketService.swift; sourceTree = "<group>"; };
613614
3AA2D5F9280EAF5D000ED971 /* AdamantSocketService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdamantSocketService.swift; sourceTree = "<group>"; };
614615
4A4D67BD3DC89C07D1351248 /* Pods-AdmCore.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AdmCore.release.xcconfig"; path = "Target Support Files/Pods-AdmCore/Pods-AdmCore.release.xcconfig"; sourceTree = "<group>"; };
616+
553B1283281C6EE100FFF24C /* GCDUtilites.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GCDUtilites.swift; sourceTree = "<group>"; };
615617
6403F5DD22723C6800D58779 /* DashMainnet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DashMainnet.swift; sourceTree = "<group>"; };
616618
6403F5DF22723F6400D58779 /* DashWalletRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DashWalletRouter.swift; sourceTree = "<group>"; };
617619
6403F5E122723F7500D58779 /* DashWallet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DashWallet.swift; sourceTree = "<group>"; };
@@ -1773,6 +1775,7 @@
17731775
E927171D20C04613002BB9A6 /* UIColor+hex.swift */,
17741776
E9D1BE19211DA25300E86B72 /* UIView+constraints.swift */,
17751777
E935C84C22AC06E500A8CA2F /* RichMessageTools.swift */,
1778+
553B1283281C6EE100FFF24C /* GCDUtilites.swift */,
17761779
);
17771780
path = Helpers;
17781781
sourceTree = "<group>";
@@ -2450,6 +2453,7 @@
24502453
6449BA69235CA0930033B936 /* ERC20TransferViewController.swift in Sources */,
24512454
E9E7CD8B20026B0600DFC4DB /* AccountService.swift in Sources */,
24522455
640EFAA42558613A00E9724B /* EthProvider.swift in Sources */,
2456+
553B1284281C6EE100FFF24C /* GCDUtilites.swift in Sources */,
24532457
648CE3AC229AD2190070A2CC /* DashTransferViewController.swift in Sources */,
24542458
640EFA9C2558613200E9724B /* AdamantProvider.swift in Sources */,
24552459
64F085D920E2D7600006DE68 /* AdmTransactionsViewController.swift in Sources */,

Adamant/Services/AdamantDialogService.swift

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ class AdamantDialogService: DialogService {
3232
extension AdamantDialogService {
3333
func present(_ viewController: UIViewController, animated: Bool, completion: (() -> Void)?) {
3434
viewController.modalPresentationStyle = .overFullScreen
35-
if Thread.isMainThread {
36-
getTopmostViewController()?.present(viewController, animated: animated, completion: completion)
37-
} else {
38-
DispatchQueue.main.async { [weak self] in
39-
self?.getTopmostViewController()?.present(viewController, animated: animated, completion: completion)
40-
}
35+
36+
DispatchQueue.onMainAsync { [weak self] in
37+
self?.getTopmostViewController()?.present(viewController, animated: animated, completion: completion)
4138
}
4239
}
4340

@@ -86,12 +83,8 @@ extension AdamantDialogService {
8683
}
8784

8885
func dismissProgress() {
89-
if Thread.isMainThread {
86+
DispatchQueue.onMainAsync {
9087
FTIndicator.dismissProgress()
91-
} else {
92-
DispatchQueue.main.async {
93-
FTIndicator.dismissProgress()
94-
}
9588
}
9689
}
9790

@@ -104,12 +97,8 @@ extension AdamantDialogService {
10497
}
10598

10699
func showError(withMessage message: String, error: Error? = nil) {
107-
if Thread.isMainThread {
108-
internalShowError(withMessage: message, error: error)
109-
} else {
110-
DispatchQueue.main.async { [weak self] in
111-
self?.internalShowError(withMessage: message, error: error)
112-
}
100+
DispatchQueue.onMainAsync { [weak self] in
101+
self?.internalShowError(withMessage: message, error: error)
113102
}
114103
}
115104

@@ -372,12 +361,9 @@ extension AdamantDialogService {
372361

373362
alert.addAction(UIAlertAction(title: String.adamantLocalized.alert.cancel, style: .cancel, handler: nil))
374363
alert.modalPresentationStyle = .overFullScreen
375-
if Thread.isMainThread {
376-
present(alert, animated: true, completion: nil)
377-
} else {
378-
DispatchQueue.main.async { [weak self] in
379-
self?.present(alert, animated: true, completion: nil)
380-
}
364+
365+
DispatchQueue.onMainAsync { [weak self] in
366+
self?.present(alert, animated: true, completion: nil)
381367
}
382368
}
383369
}
@@ -504,12 +490,9 @@ extension AdamantDialogService {
504490
alertVC.alertActionStackViewHeightConstraint.constant = 50
505491
}
506492
alertVC.modalPresentationStyle = .overFullScreen
507-
if Thread.isMainThread {
508-
present(alertVC, animated: true, completion: nil)
509-
} else {
510-
DispatchQueue.main.async { [weak self] in
511-
self?.present(alertVC, animated: true, completion: nil)
512-
}
493+
494+
DispatchQueue.onMainAsync { [weak self] in
495+
self?.present(alertVC, animated: true, completion: nil)
513496
}
514497
}
515498
}

Adamant/Services/AdamantNotificationService.swift

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,7 @@ extension AdamantNotificationsService {
155155
}
156156
}
157157

158-
if Thread.isMainThread {
159-
callback()
160-
} else {
161-
DispatchQueue.main.sync {
162-
callback()
163-
}
164-
}
158+
DispatchQueue.onMainSync(callback)
165159
}
166160
}
167161

@@ -175,12 +169,8 @@ extension AdamantNotificationsService {
175169
content.sound = UNNotificationSound(named: UNNotificationSoundName("notification.mp3"))
176170

177171
if let number = type.badge {
178-
if Thread.isMainThread {
172+
DispatchQueue.onMainSync {
179173
content.badge = NSNumber(value: UIApplication.shared.applicationIconBadgeNumber + backgroundNotifications + number)
180-
} else {
181-
DispatchQueue.main.sync {
182-
content.badge = NSNumber(value: UIApplication.shared.applicationIconBadgeNumber + backgroundNotifications + number)
183-
}
184174
}
185175

186176
if isBackgroundSession {
@@ -222,12 +212,8 @@ extension AdamantNotificationsService {
222212
securedStore.remove(StoreKey.notificationsService.customBadgeNumber)
223213
}
224214

225-
if Thread.isMainThread {
215+
DispatchQueue.onMainAsync {
226216
UIApplication.shared.applicationIconBadgeNumber = appIconBadgeNumber
227-
} else {
228-
DispatchQueue.main.async {
229-
UIApplication.shared.applicationIconBadgeNumber = appIconBadgeNumber
230-
}
231217
}
232218
}
233219

Adamant/Services/DataProviders/AdamantAccountsProvider.swift

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -162,24 +162,16 @@ class AdamantAccountsProvider: AccountsProvider {
162162
if let context = context {
163163
// viewContext only on MainThread
164164
if context == stack.container.viewContext {
165-
if Thread.isMainThread {
165+
DispatchQueue.onMainSync {
166166
acc = (try? context.fetch(request))?.first
167-
} else {
168-
DispatchQueue.main.sync {
169-
acc = (try? context.fetch(request))?.first
170-
}
171167
}
172168
} else {
173169
acc = (try? context.fetch(request))?.first
174170
}
175171
} else {
176172
// viewContext only on MainThread
177-
if Thread.isMainThread {
173+
DispatchQueue.onMainSync {
178174
acc = (try? stack.container.viewContext.fetch(request))?.first
179-
} else {
180-
DispatchQueue.main.sync {
181-
acc = (try? stack.container.viewContext.fetch(request))?.first
182-
}
183175
}
184176
}
185177

@@ -418,32 +410,24 @@ extension AdamantAccountsProvider {
418410
*/
419411

420412
private func createCoreDataAccount(with address: String, publicKey: String) -> CoreDataAccount {
421-
let coreAccount: CoreDataAccount
422-
if Thread.isMainThread {
423-
coreAccount = createCoreDataAccount(with: address, publicKey: publicKey, context: stack.container.viewContext)
424-
} else {
425-
var acc: CoreDataAccount!
426-
DispatchQueue.main.sync {
427-
acc = createCoreDataAccount(with: address, publicKey: publicKey, context: stack.container.viewContext)
428-
}
429-
coreAccount = acc
430-
}
413+
var coreAccount: CoreDataAccount!
431414

415+
DispatchQueue.onMainSync {
416+
coreAccount = createCoreDataAccount(
417+
with: address,
418+
publicKey: publicKey,
419+
context: stack.container.viewContext
420+
)
421+
}
432422
return coreAccount
433423
}
434424

435425
private func createCoreDataAccount(from account: AdamantAccount) -> CoreDataAccount {
436-
let coreAccount: CoreDataAccount
437-
if Thread.isMainThread {
426+
var coreAccount: CoreDataAccount!
427+
428+
DispatchQueue.onMainSync {
438429
coreAccount = createCoreDataAccount(from: account, context: stack.container.viewContext)
439-
} else {
440-
var acc: CoreDataAccount!
441-
DispatchQueue.main.sync {
442-
acc = createCoreDataAccount(from: account, context: stack.container.viewContext)
443-
}
444-
coreAccount = acc
445430
}
446-
447431
return coreAccount
448432
}
449433

Adamant/Stories/Chats/ChatListViewController.swift

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,10 @@ class ChatListViewController: UIViewController {
306306
return
307307
}
308308

309-
if Thread.isMainThread {
310-
busyBackgroundView.isHidden = false
311-
busyBackgroundView.alpha = 1.0
312-
busyIndicatorView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
313-
} else {
314-
DispatchQueue.main.async {
315-
self.busyBackgroundView.isHidden = false
316-
self.busyBackgroundView.alpha = 1.0
317-
self.busyIndicatorView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
318-
}
309+
DispatchQueue.onMainAsync {
310+
self.busyBackgroundView.isHidden = false
311+
self.busyBackgroundView.alpha = 1.0
312+
self.busyIndicatorView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
319313
}
320314

321315
return
@@ -343,14 +337,9 @@ class ChatListViewController: UIViewController {
343337
self.busyBackgroundView.isHidden = false
344338
}
345339

346-
if Thread.isMainThread {
340+
DispatchQueue.onMainAsync {
347341
initialValues()
348342
UIView.animate(withDuration: 0.2, animations: animations, completion: completion)
349-
} else {
350-
DispatchQueue.main.async {
351-
initialValues()
352-
UIView.animate(withDuration: 0.2, animations: animations, completion: completion)
353-
}
354343
}
355344
}
356345
}

Adamant/Stories/Chats/ChatViewController.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -894,20 +894,12 @@ extension ChatViewController: TransferViewControllerDelegate, ComplexTransferVie
894894
private func dismissTransferViewController(andPresent viewController: UIViewController?) {
895895
fixKeyboardInsets = true
896896

897-
if Thread.isMainThread {
898-
dismiss(animated: true, completion: nil)
897+
DispatchQueue.onMainAsync { [weak self] in
898+
self?.dismiss(animated: true, completion: nil)
899899

900-
if let viewController = viewController, let nav = navigationController {
900+
if let viewController = viewController, let nav = self?.navigationController {
901901
nav.pushViewController(viewController, animated: true)
902902
}
903-
} else {
904-
DispatchQueue.main.async { [weak self] in
905-
self?.dismiss(animated: true, completion: nil)
906-
907-
if let viewController = viewController, let nav = self?.navigationController {
908-
nav.pushViewController(viewController, animated: true)
909-
}
910-
}
911903
}
912904
}
913905
}

Adamant/Stories/Chats/NewChatViewController.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,8 @@ class NewChatViewController: FormViewController {
180180
}
181181

182182
if let done = self?.navigationItem.rightBarButtonItem {
183-
if Thread.isMainThread {
183+
DispatchQueue.onMainAsync {
184184
done.isEnabled = text.count > 6
185-
} else {
186-
DispatchQueue.main.async {
187-
done.isEnabled = text.count > 6
188-
}
189185
}
190186
}
191187
} else {
@@ -375,12 +371,8 @@ extension NewChatViewController {
375371
AVCaptureDevice.requestAccess(for: .video) { [weak self] (granted: Bool) in
376372
if granted, let qrReader = self?.qrReader {
377373
qrReader.modalPresentationStyle = .overFullScreen
378-
if Thread.isMainThread {
374+
DispatchQueue.onMainAsync {
379375
self?.present(qrReader, animated: true, completion: nil)
380-
} else {
381-
DispatchQueue.main.async {
382-
self?.present(qrReader, animated: true, completion: nil)
383-
}
384376
}
385377
} else {
386378
return

Adamant/Stories/Chats/SearchResultsViewController.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,10 @@ class SearchResultsViewController: UITableViewController {
148148
cell.avatarImageView.tintColor = UIColor.adamant.primary
149149
} else {
150150
if let address = partner.publicKey {
151-
if Thread.isMainThread {
152-
let image = self.avatarService.avatar(for: address, size: 200)
151+
let image = self.avatarService.avatar(for: address, size: 200)
152+
153+
DispatchQueue.onMainAsync {
153154
cell.avatarImage = image
154-
} else {
155-
let image = self.avatarService.avatar(for: address, size: 200)
156-
DispatchQueue.main.async {
157-
cell.avatarImage = image
158-
}
159155
}
160156

161157
cell.avatarImageView.roundingMode = .round

Adamant/Stories/Delegates/DelegatesListViewController.swift

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -413,31 +413,17 @@ extension DelegatesListViewController {
413413
let newVotesColor = changes.count > maxVotes ? UIColor.adamant.alert : UIColor.adamant.primary
414414
let totalVotesColor = totalVoted > maxTotalVotes ? UIColor.adamant.alert : UIColor.adamant.primary
415415

416-
417-
if Thread.isMainThread {
418-
upVotesLabel.text = String(upvoted)
419-
downVotesLabel.text = String(downvoted)
420-
newVotesLabel.text = "\(changes.count)/\(maxVotes)"
421-
totalVotesLabel.text = "\(totalVoted)/\(maxTotalVotes)"
416+
DispatchQueue.onMainAsync { [weak self] in
417+
guard let self = self else { return }
422418

423-
voteBtn.isEnabled = votingEnabled
424-
newVotesLabel.textColor = newVotesColor
425-
totalVotesLabel.textColor = totalVotesColor
426-
} else {
427-
let changes = changes.count
428-
let max = maxVotes
429-
let totalMax = maxTotalVotes
419+
self.upVotesLabel.text = "\(upvoted)"
420+
self.downVotesLabel.text = "\(downvoted)"
421+
self.newVotesLabel.text = "\(changes.count)/\(self.maxVotes)"
422+
self.totalVotesLabel.text = "\(totalVoted)/\(self.maxTotalVotes)"
430423

431-
DispatchQueue.main.async { [unowned self] in
432-
self.upVotesLabel.text = "\(upvoted)"
433-
self.downVotesLabel.text = "\(downvoted)"
434-
self.newVotesLabel.text = "\(changes)/\(max)"
435-
self.totalVotesLabel.text = "\(totalVoted)/\(totalMax)"
436-
437-
self.voteBtn.isEnabled = votingEnabled
438-
self.newVotesLabel.textColor = newVotesColor
439-
self.totalVotesLabel.textColor = totalVotesColor
440-
}
424+
self.voteBtn.isEnabled = votingEnabled
425+
self.newVotesLabel.textColor = newVotesColor
426+
self.totalVotesLabel.textColor = totalVotesColor
441427
}
442428
}
443429
}

Adamant/Stories/Login/LoginViewController+QR.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,11 @@ extension LoginViewController {
2424
case .notDetermined:
2525
AVCaptureDevice.requestAccess(for: .video) { [weak self] (granted: Bool) in
2626
if granted {
27-
if Thread.isMainThread {
27+
DispatchQueue.onMainAsync {
2828
let reader = QRCodeReaderViewController.adamantQrCodeReader()
2929
reader.delegate = self
3030
reader.modalPresentationStyle = .overFullScreen
3131
self?.present(reader, animated: true, completion: nil)
32-
} else {
33-
DispatchQueue.main.async {
34-
let reader = QRCodeReaderViewController.adamantQrCodeReader()
35-
reader.delegate = self
36-
reader.modalPresentationStyle = .overFullScreen
37-
self?.present(reader, animated: true, completion: nil)
38-
}
3932
}
4033
} else {
4134
return

0 commit comments

Comments
 (0)