Traitement asynchrone à l'aide de Promise
PC | MacBook Air(13-inch,2017) |
PC OS | macOS Catalina(ver 10.15.6) |
IDE | Xcode(ver 12.0.1) |
iPhone | SE(2nd Generation) |
iPhone OS | ver 14.0.1 |
Swift | 5.3 |
① Connectez MacBook Air et iPhone avec un câble USB
(2) Créez une application de projet appelée Sample
sur le bureau en utilisant Xcode.
③ Sample
a déjà installé PromiseKit
en utilisant CocoaPods
.
④ Cette fois, écrivez le code dans ViewController.swift
dans Sample
.
ViewController.swift
import UIKit
import PromiseKit
class ViewController: UIViewController {
override func viewDidLoad() {
let promise = Promise<String> { value in
//En traitement
}.done { value in
//Exécuter en cas de succès
}.catch { error in
//Exécuter quand il échoue
}.finally {
//Dernier tour
}
}
}
ViewController.swift
import UIKit
import PromiseKit
class ViewController: UIViewController {
override func viewDidLoad() {
print("-----promesse d'ici-----")
let promise = Promise<String> { value in
//En traitement
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
value.fulfill("sample1")
print("\(Date()) [1]:\(value)")
}
DispatchQueue.main.asyncAfter(deadline: .now() + 4.0) {
value.fulfill("sample2")
print("\(Date()) [2]:\(value)")
}
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
value.fulfill("sample3")
print("\(Date()) [3]:\(value)")
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
value.fulfill("sample4")
print("\(Date()) [4]:\(value)")
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
value.fulfill("sample5")
print("\(Date()) [5]:\(value)")
}
print("\(Date()) [6]:\(value)")
}.done { value in
//Exécuter en cas de succès
print("\(Date()) [7]:\(value)")
}.catch { error in
//Exécuter quand il échoue
print("\(Date()) [8]:\(error.localizedDescription)")
}.finally {
//Dernier tour
print("\(Date()) [9]: Termine le traitement")
}
print("-----promis jusqu'ici-----")
}
}
Journal Xcode
-----promesse d'ici-----
2020-10-22 13:16:28 +0000 [6]:PromiseKit.Resolver<Swift.String>
-----promis jusqu'ici-----
2020-10-22 13:16:29 +0000 [5]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:16:29 +0000 [7]:sample5
2020-10-22 13:16:29 +0000 [9]: Termine le traitement
2020-10-22 13:16:30 +0000 [4]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:16:31 +0000 [3]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:16:32 +0000 [2]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:16:33 +0000 [1]:PromiseKit.Resolver<Swift.String>
ViewController.swift
import UIKit
import PromiseKit
enum SampleError: Error {
case error1
case error2
case error3
}
class ViewController: UIViewController {
override func viewDidLoad() {
print("-----promesse d'ici-----")
let promise = Promise<String> { value in
//En traitement
value.reject(SampleError.error1)
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
value.fulfill("sample1")
print("\(Date()) [1]:\(value)")
}
DispatchQueue.main.asyncAfter(deadline: .now() + 4.0) {
value.fulfill("sample2")
print("\(Date()) [2]:\(value)")
}
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
value.fulfill("sample3")
print("\(Date()) [3]:\(value)")
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
value.fulfill("sample4")
print("\(Date()) [4]:\(value)")
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
value.fulfill("sample5")
print("\(Date()) [5]:\(value)")
}
print("\(Date()) [6]:\(value)")
}.done { value in
//Exécuter en cas de succès
print("\(Date()) [7]:\(value)")
}.catch { error in
//Exécuter quand il échoue
print("\(Date()) [8]:\(error.localizedDescription)")
}.finally {
//Dernier tour
print("\(Date()) [9]: Termine le traitement")
}
print("-----promis jusqu'ici-----")
}
}
Journal Xcode
-----promesse d'ici-----
2020-10-22 13:26:04 +0000 [6]:PromiseKit.Resolver<Swift.String>
-----promis jusqu'ici-----
2020-10-22 13:26:04 +0000 [8]:The operation couldn’t be completed. (Sample.SampleError error 0.)
2020-10-22 13:26:04 +0000 [9]: Termine le traitement
2020-10-22 13:26:05 +0000 [5]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:26:06 +0000 [4]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:26:07 +0000 [3]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:26:08 +0000 [2]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:26:09 +0000 [1]:PromiseKit.Resolver<Swift.String>
ViewController.swift
import UIKit
import PromiseKit
enum SampleError: Error {
case error1
case error2
case error3
}
class ViewController: UIViewController {
override func viewDidLoad() {
print("-----promesse d'ici-----")
let promise = Promise<String> { value in
//En traitement
value.fulfill("sample1")
print("\(Date()) [1]:\(value)")
value.fulfill("sample2")
print("\(Date()) [2]:\(value)")
value.fulfill("sample3")
print("\(Date()) [3]:\(value)")
value.fulfill("sample4")
print("\(Date()) [4]:\(value)")
value.fulfill("sample5")
print("\(Date()) [5]:\(value)")
print("\(Date()) [6]:\(value)")
}.done { value in
//Exécuter en cas de succès
print("\(Date()) [7]:\(value)")
}.catch { error in
//Exécuter quand il échoue
print("\(Date()) [8]:\(error.localizedDescription)")
}.finally {
//Dernier tour
print("\(Date()) [9]: Termine le traitement")
}
print("-----promis jusqu'ici-----")
}
}
Journal Xcode
-----promesse d'ici-----
2020-10-22 13:29:03 +0000 [1]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:29:03 +0000 [2]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:29:03 +0000 [3]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:29:03 +0000 [4]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:29:03 +0000 [5]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:29:03 +0000 [6]:PromiseKit.Resolver<Swift.String>
-----promis jusqu'ici-----
2020-10-22 13:29:03 +0000 [7]:sample1
2020-10-22 13:29:03 +0000 [9]: Termine le traitement
ViewController.swift
import UIKit
import PromiseKit
enum SampleError: Error {
case error1
case error2
case error3
}
class ViewController: UIViewController {
override func viewDidLoad() {
print("-----promesse d'ici-----")
let promise = Promise<String> { value in
//En traitement
value.reject(SampleError.error1)
value.fulfill("sample1")
print("\(Date()) [1]:\(value)")
value.fulfill("sample2")
print("\(Date()) [2]:\(value)")
value.fulfill("sample3")
print("\(Date()) [3]:\(value)")
value.fulfill("sample4")
print("\(Date()) [4]:\(value)")
value.fulfill("sample5")
print("\(Date()) [5]:\(value)")
print("\(Date()) [6]:\(value)")
}.done { value in
//Exécuter en cas de succès
print("\(Date()) [7]:\(value)")
}.catch { error in
//Exécuter quand il échoue
print("\(Date()) [8]:\(error.localizedDescription)")
}.finally {
//Dernier tour
print("\(Date()) [9]: Termine le traitement")
}
print("-----promis jusqu'ici-----")
}
}
Journal Xcode
-----promesse d'ici-----
2020-10-22 13:33:08 +0000 [1]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:33:08 +0000 [2]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:33:08 +0000 [3]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:33:08 +0000 [4]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:33:08 +0000 [5]:PromiseKit.Resolver<Swift.String>
2020-10-22 13:33:08 +0000 [6]:PromiseKit.Resolver<Swift.String>
-----promis jusqu'ici-----
2020-10-22 13:33:08 +0000 [8]:The operation couldn’t be completed. (Sample.SampleError error 0.)
2020-10-22 13:33:08 +0000 [9]: Termine le traitement
Recommended Posts