[iOS_MyInventory] 상품등록, RxSwift 사용해 보기
RxSwift
상태값 변화를 관찰 혹은 비동기 프로그래밍을 위한 API.
RxSwift 개념잡기에 도움되었던, 마기님 포스팅
https://magi82.github.io/ios-rxswift-01/
RxSwift 알아보기(ReactiveX 에 대해서) - 01 – 마기의 개발 블로그 – 즐겁게 개발을 하고 싶은 욕심 �
안녕하세요 마기입니다. 오랜만에 포스팅을 합니다. 그동안 여러 일로 정신이 없었습니다. 😭 다시 힘내서 자주 포스팅 할 예정입니다. 이번 시간에는 핫한 시기를 넘어서 반 필수적으로 알아��
magi82.github.io
정리 잘 되어있는 wade 님 브런치
https://brunch.co.kr/@tilltue/4
RxSwift, Subject 알아보기
Publish, Behavior, Replay Subject. | publishSubject* 이 포스트는 RxSwift 4.3.1, swift 4.2 버전을 기준으로 작성되었습니다.* RxSwift 의 PublishSubject.swift, BehaviorSubject.swift, ReplaySubject.swift 내용ReactiveX Observable 에는 "Ho
brunch.co.kr
RxSwift 를 적용하게 된 이유
상품등록 페이지는 UICollectionView 를 이용하여 각각의 셀들을 보여주고 있는 형태인데,
셀에 입력한 값을 알기위함이나 이벤트에 의한 셀의 변화를 부모인 ViewController 에 전달하기 위해서는 delegate 프로토콜을 통해야만 했다.
커스텀으로 delegate 를 만드는 경우도 생기게 되었는데,
프로토콜 선언-프로토콜 채택-이벤트연결(delegte=self) 까지... 많은 단계를 거쳐야 했다.
하지만! RxSwift 에서는 event 가 일어나는 대상과 subject 를 바인딩 해서 입력값을 가져올 수 있다.
그리고 이벤트가 발생하는 경우, 관찰자(Observer) 를 통해 값의 변화를 알 수 있다.
let nameProperty = BehaviorSubject(value: "")
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellType = self.cellList[indexPath.section].rows[indexPath.row]
switch cellType {
case .name:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellType.getCellId(), for: indexPath) as! AddItemNameCell
cell.nameTextField.delegate = self
cell.nameTextField.tag = indexPath.row
cell.nameTextField.rx.text.orEmpty
.map { $0 as String }
.bind(to: self.nameProperty)
.disposed(by: disposeBag)
self.nameProperty.subscribe( onNext: { newValue in
print(newValue)
}).disposed(by: disposeBag)
return cell
}
}
Realm에 Textfield 입력값 넣기
textfield 에 입력한 제품이름 값을 받아서 제대로 넣어지는지 테스트! 잘 들어가는듯 하다. 👏🏻
RealmSwift 에서 이미지 데이터는 16MB 까지만 들어가므로, 압축해서 넣어줘야 겠다.
이제, 나머지 필드값도 작업...