Combine Start With Subject

Combine Start With Subject

Subject からはじめる Combine

Mobile Act ONLINE #2
https://mobileact.connpass.com/event/189045/

登壇時の指摘を受けて一部修正済(P.13)

0b26590a0a8b0f1da140ed5de9b68379?s=128

USAMI Kosuke

October 09, 2020
Tweet

Transcript

  1. 4VCKFDU͔Β͸͡ΊΔ $PNCJOF Ӊࠤݟެี  1/17

  2. ࣗݾ঺հ w Ӊࠤݟެี w 鳍鲟鳫鳢鳣גࣜձࣾJ04鲠鳫鲰鳃鲚 w .PCJMF"DU0/-*/&鱬$PNCJOF鱳࿩鲗鱜鲃鱜鱤 2/17

  3. ٕज़ಉਓࢽͷ঺հ w ٕज़ॻయ鱬ٕज़ಉਓࢽ鲗ग़鱜鲃鱜鱤 w ݱࡏɺҎԼ鱬ൢചத w ٕज़ॻయ鳖鱅鲩鲻鳀 w #005) w

    ;FOO 3/17
  4. ࠓճͷ࿩ w ઌ鲀鱮鱳ʮ$PNCJOF鲗鱴鱝鲆鲍鱋ʯ鲗ॻ鱉鱤ࡍ鱳ཪ࿩ w 9DPEF1MBZHSPVOE鱳׆༻ w 4VCKFDU鱳׆༻ 4/17

  5. $PNCJOFͱ͸ w 4XJGU鱳鳢鲚鲧鲾鲛鳎鳏鳥鲨鳡鳗鳫鲨鱳鳍鳤鱅鳘鳧鱅鲧 w ྨࣅ鱳鲇鱳3Y4XJGU3FBDUJWF4XJGU w ඪ४鳍鳤鱅鳘鳧鱅鲧鱳鱷鱭鱩鱰Ճ鲔鱨鱤 w ࠓޙɺ࠾༻ࣄྫ鱑૿鱍鲐鱭ࢥ鲔鲑鲐 w

    ֤छ鱳ඇಉظ鲜鳑鳫鳀鱳鳇鳫鳁鳢鳫鲨鱰࢖鱋鱘鱭鱑鱬鱒鲐 5/17
  6. $PNCJOFͷෑډΛԼ͍͛ͨ w 鳢鲚鲧鲾鲛鳎鳏鳥鲨鳡鳗鳫鲨鱨鱫ɾɾɾ w ෑډ鱑ߴ鱉ɺֶश鲫鲱鳀鱑ߴ鱉ɺ鱭鱉鱋鲜鳙鱅鲰 w 鱜鱐鱜ɺ࣮鱴鱢鲘鱯鱰೉鱜鱔鱯鱉 w 鱢鱘鱬ɺෑډ鲗Լ鱗鱤鱉 6/17

  7. ֶͿํ਑ 鳏鳥鲨鳡鳖鱬鱇鲑鱵ɺݴ༿鱬鱇鲑鱘鲑આ໌鱞鲐લ鱰ɺ ࣮ࡍ鱰鲫鱅鳁鲗ॻ鱉鱫ಈ鱐鱜鱫鲄鲐鱳鱑ཧղ鱑ૣ鱉鱥鲒鱋 Šʮ$PNCJOF鲗鱴鱝鲆鲍鱋ʯ鲍鲏ൈਮ w 9DPEF1MBZHSPVOE鱬鱞鱕鱰ಈ鱐鱠鲐鲫鱅鳁鲗༻ҙ w 鱞鱕ಈ鱐鱠鲐鲍鱋鱰鱞鲐鱘鱭鱬ෑډ鲗Լ鱗鲐ૂ鱉 7/17

  8. 9DPEF1MBZHSPVOEͰ͙͢ಈ͔ͤΔྫ let subject = PassthroughSubject<String, Never>() subject .sink { value

    in print("Received value:", value) } subject.send("͋") subject.send("͍") subject.send("͏") 8/17
  9. 1VCMJTIFSʹ4VCKFDUΛ࠾༻ w 1VCMJTIFSඇಉظ鲜鳑鳫鳀鲗ૹ৴鱞鲐鲇鱳 w 鲃鱟鱴鱘鲑鲗஌鲐鱳鱑$PNCJOF鲗஌鲐ୈҰา w ࠷ॳ鱳1VCMJTIFS鱭鱜鱫PassthroughSubject鲗࠾༻ w 鱘鱳鲉鲏ํ鱴ɺଞ鱳ղઆ鱬鱴鱇鲃鲏ݟ鱐鱖鱯鱉 w

    鱯鱡4VCKFDU鲗࠾༻鱜鱤鱳鱐ʁ 9/17
  10. /PUJpDBUJPO$FOUFSͷྫ w $PNCJOF鱳ղઆ鱬鲍鱔࢖鲔鲑鲐 let myNotification = Notification.Name("MyNotification") let publisher =

    NotificationCenter.default.publisher(for: myNotification) publisher .sink { value in print("Received value:", value) } 10/17
  11. /PUJpDBUJPO$FOUFSͷྫͷར఺ͱܽ఺ w ར఺ɿNotificationCenter鱴஌鱨鱫鱉鲐ਓ鱑ଟ鱔ɺಋೖ鱭 鱜鱫෼鱐鲏鲉鱞鱉 w ར఺ɿඪ४鱬publisher鳙鲵鲻鳁鱑༻ҙ鱚鲑鱫鱉鱫ศར w ܽ఺ɿ୯७鱯鲜鳑鳫鳀鱜鱐ൃੜ鱚鱠鲎鲑鱯鱉 11/17

  12. $PNCJOFͷΠϕϯτ w 鲜鳑鳫鳀鱴ҎԼ鱳छྨ w ஋ w 鲜鳑鳫鳀׬ྃʢ.finishedʣ w 鲠鳡鱅ʢ.failureʣ w

    鱘鲑鲎鲗ࣗ༝鱰ൃੜ鱚鱠鲎鲑鲐鲀鱋鱑ֶ鱸鲉鱞鱉 12/17
  13. 4VCKFDUͷྫ let subject = PassthroughSubject<String, MyError>() subject.send("͋") subject.send(completion: .finished) subject.send(completion:

    .failure(.failed)) enum MyError: Error { case failed } 13/17
  14. 4VCKFDUͷྫͷར఺ w छྨ鱳鲜鳑鳫鳀鲗؆୯鱰ൃੜ鱚鱠鲎鲑鲐 w NotificationCenter鱴஋鱳鲜鳑鳫鳀鱜鱐鱯鱐鱨鱤 w 9DPEF1MBZHSPVOE鱬鲫鱅鳁鲗ॻ鱉鱫ֶ鱻鱭鱉鱋ํ਑鱰߹க w 鱥鱐鲎4VCKFDU鲗࠾༻鱜鱤 14/17

  15. 4VCKFDUͷ஫ҙ఺ w Ұൠత鱯1VCMJTIFS鱴鲜鳑鳫鳀鱑ྲྀ鲑鱫鱔鲐鱥鱖 w 4VCKFDU鱴send鱬鲜鳑鳫鳀鲗ҙਤత鱰ൃੜ鱚鱠鲐鱘鱭鱑鱬鱒鲐 w ศར鱯ػೳ鱥鱑ɺ1VCMJTIFS鱭鱜鱫鱴༨෼鱯໾ׂ w 鱘鱳఺鱴4VCKFDU鱑1VCMJTIFS鱳ྫ鱭鱜鱫ྑ鱔鱯鱉ཧ༝鱰鱯鲐 15/17

  16. 4VCKFDUͷܕফڈ w Subject鲗AnyPublisherܕ鱭鱜鱫ݟ鱠鲐 let subject = PassthroughSubject<String, Never>() let publisher

    = subject.eraseToAnyPublisher() w 鲜鳑鳫鳀ड৴ଆ鱰鱴publisher鱳鲀鱋鲗࢖鲔鱠鲐 w ࣮ࡍ鱳鲚鳏鳢ઃܭ鱬鱴ɺ্ड़鱳ํ๏鲗ར༻鱞鲐鱳鱑޷鲃鱜鱉 16/17
  17. ·ͱΊ w $PNCJOF鲗ֶ鱻鱰鱴9DPEF1MBZHSPVOE鱑ྑ鱉 w 4VCKFDU鱴9DPEF1MBZHSPVOE鱬鱳ֶ鱸鱰޲鱉鱫鱉鲐 w 鱤鱥鱜Ұൠత鱯1VCMJTIFS鱭ҟ鱯鲐఺鲇鱇鲐鱳鱬஫ҙ 17/17