μ€λμ νμ΄μ΄λ² μ΄μ€ νΈλμμ μ λν΄μ μμκ° λ΄ μλ€.
λ¨Όμ νΈλμμ μ΄λ 1κ° μ΄μμ λ¬Έμμ λν μ½κΈ° λ° μ°κΈ° μμ μ μ§ν©μ΄λΌκ³ νμ΄μ΄λ² μ΄μ€ λ¬Έμμ λνλμλλ°μ.
νΈλμμ μ΄λ μ©μ΄λ λ¨Όμ λ°μ΄ν°λ₯Ό μ λ ₯νλ λμμ λ€λ₯Έ λ°μ΄ν°λ₯Ό μ λ ₯νμ§ λͺ»νκ² νλ λ°©μμ λλ€.
μ½κ² μλ₯Ό λ€μ΄ μ€λͺ νλ©΄
μΈμ€νκ·Έλ¨μ μ’μμ ννΈλͺ¨μ λ²νΌμ λλ₯΄λ κ²κ³Ό κ°μ΅λλ€.
μ’μμ ννΈλͺ¨μμ λ§μ½ λμμ μ¬λ¬μ¬λμ΄ λλ μ λ λ°μ΄ν°κ° μμλ μκ° μμ΅λλ€.
κ·Έλμ ν λ°μ΄ν°κ° λ¨Όμ μλ£λκΈ° μ κΉμ§ λ€λ₯Έ λ°μ΄ν°μ μ λ ₯μ κΈμ§νκ³ μλ£κ° λλ©΄ κ·Έ λ€μ λ°μ΄ν°λ₯Ό μ λ ₯νλ νμμΌλ‘ μ§νλ©λλ€.
κ·ΈλΌ νλ² μ’μμ κΈ°λ₯μ λ§λ€μ΄λ³ΌκΉμ?
λ¨Όμ νμ΄μ΄μ€ν μ΄μμ μΈμ€νκ·Έλ¨μ΄λΌλ μ½λ μ μ μ’μμλΌλ λ¬Έμλ₯Ό λ§λ λ€ νλκ°μΌλ‘ likeμ κ°―μλ₯Ό λ§λ€μ΄μ€λλ€.
κ·Έ λ€μ Xcodeμ νλ‘μ νΈλ₯Ό λ§λ€μ΄μ£Όμ ν 뷰컨νΈλ‘€λ¬μ μλμ κ°μ΄ ννΈλ²νΌμ λ§λ€μ΄μ£Όμκ³
μλ νΈλμμ μ½λλ₯Ό λ£μ΄μ€λλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
import UIKit
import FirebaseFirestore
class TabbarViewController2: UIViewController {
@IBOutlet weak var heartbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func heartbtntap(_ sender: Any) {
if #available(iOS 13.0, *) {
//ννΈλ²νΌ μ΄λ―Έμ§ λ°κΏμ£ΌκΈ°
heartbtn.setBackgroundImage(UIImage(systemName: "heart.fill"), for: .normal)
let db = Firestore.firestore()
//νμ΄μ΄μ€ν μ΄ λ°μ΄ν° μ£Όμ
let ref = db.collection("instagram").document("like")
db.runTransaction({ (transaction, errorPointer) -> Any? in
let Document: DocumentSnapshot
do {
try Document = transaction.getDocument(ref)
} catch let fetchError as NSError {
errorPointer?.pointee = fetchError
return nil
}
guard let like = Document.data()?["like"] as? Int else {
let error = NSError(
domain: "AppErrorDomain",
code: -1,
userInfo: [
NSLocalizedDescriptionKey: "Unable to retrieve like from snapshot \(Document)"
]
)
errorPointer?.pointee = error
return nil
}
//νΈλμμ
μΌλ‘ μ’μμ μλ₯Ό 1λν΄μ€.
transaction.updateData(["like": like + 1], forDocument: ref)
return nil
}) { (object, error) in
if let error = error {
//μ€ν¨νμλ
print("Transaction failed: \(error)")
} else {
//μ±κ³΅νμ λ μΆλ ₯
print("Transaction successfully committed!")
}
}
}
}
}
|
cs |
ννΈλ²νΌμ λλ μ λ νΈλμμ μ μ€νν΄μ£Όμλ©΄ μλμ κ°μ΄ μ’μμ μκ° νΈλμμ μΌλ‘ μμ νκ² 1λ§νΌ μ¦κ°κ° λ©λλ€!!
λκΈ