-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
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
} |
문제는... Realm 객체의 생성 및 여기에의 접근 뿐만 아니라, 가져온 Object 혹은 List에 대한 접근 또한 thread-confined되있는데.. 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 }
} |
@Hansolkkim |
junu0516
added a commit
that referenced
this issue
Nov 17, 2022
Open
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: