iOS & Swift 공부 - Realm -> Reading Data Using Realm (Read in CRUD)
Reading Data Using Realm (Read in CRUD)
func loadCategories() {
categories = realm.objects(Category.self)
tableView.reloadData()
}
→ Unlike Core Data, fetching Realm objects is really simple, because it only requires 1 line.
If you want to fetch a Category type object, just specify it like the code above, but just remember to specify ".self" after the class name.
Also, since realm.object ( ) return a type of "Results", the "categories" must also be in this format.
var categories: Results<Category>?
- A special thing about the Results container in Realm, is that it is an auto-updating container type.
→ What this means is that we won't have to "append" new elements to the array explicitly using the append function.
let newCategory = Category()
newCategory.name = textField.text!
//self.categories.append(newCategory)
// The above code is not needed
self.save(category: newCategory)
func save(category: Category) {
do {
try realm.write {
realm.add(category)
}
} catch {
print("Error saving new category : \(error)")
}
self.tableView.reloadData()
}
In fact, the Result container type doesn't even support the "append" method.
→ As long as you saved the updated element to Realm, you don't have to explicitly append the new element to the array.
→ It's auto-updated!
Author And Source
이 문제에 관하여(iOS & Swift 공부 - Realm -> Reading Data Using Realm (Read in CRUD)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@kevinkim2586/iOS-Swift-공부-Realm-Reading-Data-Using-Realm-Read-in-CRUD저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)