Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RealmManager 코드 개선하기 #102

Open
1 of 3 tasks
junu0516 opened this issue Oct 31, 2022 · 3 comments · May be fixed by #107
Open
1 of 3 tasks

RealmManager 코드 개선하기 #102

junu0516 opened this issue Oct 31, 2022 · 3 comments · May be fixed by #107
Assignees
Labels
jed jed 진행 refactor 코드 리팩토링에 대한 이슈

Comments

@junu0516
Copy link
Collaborator

junu0516 commented Oct 31, 2022

  • 별도의 serial queue에서 RealmManager 싱글톤 객체 관리하도록 구성
  • 필요시 @threadsafe property wrapper 적용
  • Realm Migration을 위한 영역 설정
@junu0516 junu0516 self-assigned this Nov 8, 2022
@junu0516 junu0516 added refactor 코드 리팩토링에 대한 이슈 jed jed 진행 labels Nov 8, 2022
@junu0516
Copy link
Collaborator Author

RealmManager에 별도의 serial queue를 두고 아래와 같이 데이터를 읽도록 수정

func read<T: Object>(entity: T.Type, filter query: String? = nil) -> [T] {
        var result: [T] = []
        
        realmSerialQueue.async { [weak self] in
            guard let self = self else {
                self?.semaphore.signal()
                return
            }
            
            let realm = try! Realm(configuration: self.configuration, queue: self.realmSerialQueue)
            
            if let query = query {
                result = realm.objects(entity).filter(query).toArray(ofType: entity)
            } else {
                result = realm.objects(entity).toArray(ofType: entity)
            }
            
            self.semaphore.signal()
        }
        
        semaphore.wait()
        
        return result
    }

@junu0516
Copy link
Collaborator Author

문제는... Realm 객체의 생성 및 여기에의 접근 뿐만 아니라, 가져온 Object 혹은 List에 대한 접근 또한 thread-confined되있는데..
현재 GoalListRepository에서 아래와 같이 Realm에서 가져온 객체에 메인 스레드가 접근하기 때문에 여전히 런타임 에러가 발생중..

func getLatestGoals() -> [Category] {
        var categoryMap: [String:Category] = [:]

        let goalEntities = realmManager.read(entity: GoalEntity.self)
                                       .sorted { $0.createdDate < $1.createdDate }
        let categoryEntities = realmManager.read(entity: CategoryEntity.self)
        
       // 바로 여기서 Realm에서 가져온 객체에 메인 스레드가 접근하기 때문에 런타임 오류 발생
        categoryEntities.forEach {
            categoryMap[$0.title] = Category(entity: $0)
        }
        
        goalEntities.forEach {
            var goal = Goal(entity: $0)
            let goalTitle = goal.title
            let photoCount = realmManager.read(entity: PhotoEntity.self, filter: "goalTitle == '\(goalTitle)'").count
            goal.progress = photoCount
            categoryMap[$0.category]?.items.append(goal)
        }
        
        return Array(categoryMap.values).sorted { $0.createdDate < $1.createdDate }
    }

@junu0516
Copy link
Collaborator Author

@Hansolkkim
결국 semaphore 사용해서 올렸어요... 문제는 GoalListRepository도 Realm에 접근하는데, 이게 메인 스레드에서 동작해서 계속해서 불일치하네요 ㅋㅋㅋ.. 이것까지 좀 더 고민해야 될듯요 ^_ㅠ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
jed jed 진행 refactor 코드 리팩토링에 대한 이슈
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant