-
[개발일지] Flutter Bloc 상태관리 #5 비동기 처리 방식: bloc_concurrency PackageStudy/Development 2023. 4. 12. 12:16
Flutter BlocFlutter Bloc 상태관리 #3 GetX vs Cubit vs Bloc <인프런 개발하는 남자 온라인강의 수강일지>
bloc_concurrency
Ember_concurrency에서 영감을 얻어 만듦 비동기 프로그램을 간결하면서도 강력하게 만들어주는 라이브러리
Concurrent 방식
bloc의 기본 처리 방식, 연속 호출 시 이벤트간에 시간이 겹친다. 각각의 이벤트가 불필요하게 모두 실행됨.
Concurrent 방식 Sequential 방식
1번이 완료된 이후에 2번이 실행된다. 순차적으로 실행되어 각각의 이벤트를 확인 가능하지만, 더 많은 이벤트 호출 시 의도와는 다르게 이벤트들이 하나하나 모두 끝날때까지 순차적으로 계속 실행되는 경우가 발생할 수 있다.
Sequential 방식 Droppable 방식
이벤트가 발생하여 완료되는 순간까지 다른 이벤트 호출이 무시된다.
Droppable 방식 Restartable 방식
가장 최종의 클릭만 수행. 악의적인 이벤트 호출을 무시하고, 서비스 부하를 가장 줄임.
Restartable 방식 *예제
on<Event> → transformer 옵션에 함수를 넣어 간단하게 사용
class SampleBloc extends Bloc<ShowEvent, SampleState> { final SampleRepository _sampleRepository; //concurrent : bloc의 기본 처리 방식 - transfomer에 따로 적지 않아도 됨 //sequential : 동기 방식 이벤트가 종료 되면 다음것 수행 //droppable : 이전 이벤트가 처리되는 동안 들어오는 이벤트 제거 //restartable : 최근 이벤트만 수행 이전 이벤트는 제거 SampleBloc(this._sampleRepository) : super(const SampleState.init()) { // transformer 옵션에 간단하게 함수를 넣어 사용 on<IUShowEvent>(_showIu, transformer: restartable()); } void _showIu(event, emit) async { // Restartable 사용 시 동기는 계속해서 바로 실행 emit(state.copyWith(count: state.count + 1)); // Restartable 사용 시 비동기(await)는 최종 1번만 실행 var path = await _sampleRepository.getIU(state.count); emit(state.copyWith(path: path)); } @override void onTransition(Transition<ShowEvent, SampleState> transition) { super.onTransition(transition); print(transition); } }
반응형'Study > Development' 카테고리의 다른 글
[개발일지] Flutter Bloc 상태관리 #4 Bloc vs Cubit (0) 2023.04.10 [개발일지] Flutter Bloc 상태관리 #3 GetX vs Cubit vs Bloc (0) 2023.04.08 [개발일지] Flutter Bloc 상태관리 #2 Bloc Widgets (0) 2023.04.06 [개발일지] Flutter Bloc 상태관리 #1 상태관리와 Bloc (0) 2023.04.04 [개발일지] Tensorflow 딥러닝_딥러닝이론 (0) 2023.01.02