Published on

🍎 Swift - μΆ”μƒν™”λ‘œ ν˜‘μ—…ν•˜κΈ°

Authors
  • avatar
    Name
    이창쀀
    Twitter

λ°°κ²½

  • νŒ€μ›μ•„! μž‘μ—… μ–Έμ œ λλ‚˜!? λ‚˜ κ·Έκ±° ν•„μš”ν•΄!
    • λ―Έμ•ˆ! ν•˜λ£¨λŠ” 더 ν•΄μ•Όν•  것 같은데!?
    • 어….? λ‚˜ λΆ• λœ¨λŠ”λ°β€¦.!? ν”„λ‘œν† μ½œμ΄λΌλ„ 쀘!
    • μ˜€β€¦! 천잰데?

κ³Όμ •

μ˜€λŠ˜μ€ κ°„λ‹¨ν•˜κ²Œ νŒ€μ›κ³Όμ˜ ν˜‘μ—… μ‹œ μ˜μ‘΄μ„±μ„ μ΅œλŒ€ν•œ λŠμ–΄λ‚Έ 방법을 μ†Œκ°œν•΄ λ“œλ¦¬λ €κ³  ν•©λ‹ˆλ‹€.

사싀 λ„ˆλ¬΄ κ°„λ‹¨ν•˜μ§€λ§Œ.. 이걸 생각해내고 ν•˜κ³  μžˆλ‹€λŠ” μ € μžμ‹ μ΄ μ‘°κΈˆμ€ μ„±μž₯ν–ˆλ‚˜? 싢은 λͺ¨λ¨ΌνŠΈμ˜€κ±°λ“ μš”.

λΏŒλ“―ν•΄μ„œλΌλ„ λ‚¨κ²¨λ‘¬μ•Όκ² μŠ΅λ‹ˆλ‹€ γ…Žγ…Ž

final class JourneyRepositoryImplementation: JourneyRepository {
	private let storage = PersistentStorage()
	
	func startJourney(_ recordingJourney: RecordingJourney) -> Result<RecordingJourney, Error> {
		do {
			let result = await networking.request(.startJourney(recordingJourney))
			switch result {
			case .success(let startedJourney):
				self.storage.setRecordingJourney(startedJourney) // 🚧
				return .success(startedJourney)
			case .failure(let error):
				return .failure(error)
			}
		}
	}
}

자 μ œκ°€ κ΅¬ν˜„ν•˜κ³  있던 JourneyRepositoryμž…λ‹ˆλ‹€.

κ΅¬ν˜„ 쀑에 둜컬 μŠ€ν† λ¦¬μ§€μ— 데이터λ₯Ό μ €μž₯ν•˜λŠ” 둜직이 ν•„μš”ν•΄μ‘ŒμŠ΅λ‹ˆλ‹€. (🚧 λΆ€λΆ„)

κ·Έλž˜μ„œ ν•΄λ‹Ή μž‘μ—…μ„ ν•˜κ³  있던 νŒ€μ›μ—κ²Œ 상황을 μ—¬μ­€λ΄€λŠ”λ°...

final class PersistentStorage {
	func setRecordingJourney(_ recordingJourney: RecordingJourney) {
		// ...
	}
}

λ‹€λ₯Έ μž‘μ—…μ„ ν•˜μ‹œλŠλΌ ν•΄λ‹Ή λ‘œμ§μ€ 아직 μ—†λ‹€λŠ” κ²λ‹ˆλ‹€..!

이 λ•Œ μ €λŠ” μ–΄λ–»κ²Œ ν•΄μ•Όν• κΉŒμš”?

ν•΄λ‹Ή νŒ€μ›μ΄ μž‘μ—…μ„ 마칠 λ•ŒκΉŒμ§€ λ‹€λ₯Έ μž‘μ—…μ„ 해도 λ˜κ² μ§€λ§Œ..

ν•΄λ‹Ή 둜직이 λ‹Ήμž₯ ν•„μš”ν•˜λ‹€λ©΄..?

이 λ•Œ 좔상화가 제 머리속을 μŠ€μ³€μŠ΅λ‹ˆλ‹€.

ν”„λ‘œν† μ½œμ΄λΌλ„ λ¨Όμ € μ£Όμ„Έμš”!

νŒ€μ›μ—κ²Œ μš”μ²­ν•œ 것은 좔상화λ₯Ό ν•œ ν›„ ν”„λ‘œν† μ½œμ„ λ„˜κ²¨λ‹¬λΌλŠ” μž‘μ—…μ΄μ˜€μŠ΅λ‹ˆλ‹€.

λŒ€λž΅ 10λΆ„ μ •λ„μ˜ μ‹œκ°„μ΄ μ§€λ‚œ ν›„ νŒ€μ›μ€ ν”„λ‘œν† μ½œμ„ μ €μ—κ²Œ μ „λ‹¬ν•΄μ£Όμ—ˆμŠ΅λ‹ˆλ‹€.

protocol Persistable {
	func setRecordingJourney(_ recordingJourney: RecordingJourney) throws
}

였..! 변경점이 거의 없이 throw만 μΆ”κ°€λ˜μ–΄ μ™”λ„€μš”.

private let storage: Persistable

init(storage: Persistable) {
	self.storage = storage
}

try self.storage.setRecordingJourney(startedJourney)

이제 제 μ½”λ“œλŠ” 아무 문제 없이 λΉŒλ“œλ©λ‹ˆλ‹€.

(λ¬Όλ‘  μ‹€μ œ κΈ°λŠ₯은 ν•˜μ§€ μ•Šκ² μ§€λ§Œμš”)

원격 μ €μž₯μ†Œμ— Push도 κ°€λŠ₯ν•˜κ³ , CI도 μ œλŒ€λ‘œ 돌겠죠!

κ²°κ³Ό

이 κ²½ν—˜μ„ 배경으둜 μ €ν¬λŠ” ν”„λ‘œν† μ½œμ„ λ¨Όμ € μ£Όκ³ λ°›λŠ” μž‘μ—…μ„ μŠ΅κ΄€ν™” ν–ˆμŠ΅λ‹ˆλ‹€.

덕뢄에 μ‹œκ°„μ΄ λΆ€μ‘±ν•  것 κ°™μ•˜λ˜ κ΅¬ν˜„ μ‹œκ°„ μ•ˆμ— λͺ¨λ“  핡심 κΈ°λŠ₯듀을 μΆ”κ°€ν•  수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

좔상화가 ν˜‘μ—…μ˜ κ΄€μ μ—μ„œλ„ μ’‹λ‹€λŠ” 이야기λ₯Ό 읡히 λ“€μ—ˆλŠ”λ°,

이번 κΈ°νšŒμ— ν™•μ‹€ν•˜κ²Œ 체감할 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€. πŸ‘

μ°Έκ³  λ¬Έμ„œ