Is there a way to insert new items at the 0 index to the Realm container? I don't see an insert method in the Realm class.
Do I need to use Lists? If the answer is yes, how can I restructure the following code to be able to use Lists and keep the List in constant sync with the Realm container. In other words I'm having a hard time coming up with a good way to keep the Realm container and the List with the same items when adding and removing.
In the following code new items are entered at the last index. How can I restructure it to be able to insert items at the 0 index?
Model class
import RealmSwift
class Item:Object {
dynamic var productName = ""
}
Main ViewController
let realm = try! Realm()
var items : Results<Item>?
var item:Item?
override func viewDidLoad() {
super.viewDidLoad()
self.items = realm.objects(Item.self)
}
func addNewItem(){
item = Item(value: ["productName": productNameField.text!])
try! realm.write {
realm.add(item!)
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items!.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reusableCell", for: indexPath)
let data = self.items![indexPath.row]
cell.textLabel?.text = data.productName
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete{
if let item = items?[indexPath.row] {
try! realm.write {
realm.delete(item)
}
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}
}
}
Ideally this is what I would like to be able to do when inserting new items in the addNewItem() method...
item = Item(value: ["productName": inputItem.text!])
try! realm.write {
realm.insert(item!, at:0)
}
Adding a sortedIndex integer property that lets you manually control the ordering of objects is definitely one of the more popular ways to order objects in Realm, however it's quite inefficient. In order to insert an object at 0, you'll then need to loop through every other object and increment its ordering number by 1, meaning you'll end up needing to touch every object of that type in your database in order to do it.
The best practice for this sort of implementation is to create another Object model subclass that contains a List property, keep one instance of it in Realm, and to then add every object to that. List properties behave like normal arrays, so it's possible to very quick and efficient to arrange objects that way:
import RealmSwift
class ItemList: Object {
let items = List<Item>()
}
class Item: Object {
dynamic var productName = ""
}
let realm = try! Realm()
// Get the list object
let itemList = realm.objects(ItemList.self).first!
// Add a new item to it
let newItem = Item()
newItem.productName = "Item Name"
try! realm.write {
itemList.items.insert(newItem, at: 0)
}
You can then use the ItemList.items object directly as the data source for your table view.
They're at least two ways to do this:
you can manually add priority as number in your class for.ex:
class Task: Item {
dynamic var something = ""
dynamic var priority = 0
}
add to realm:
//you can make priority 2,3,4 etc - 0 will be at the top
let task = Task(something: "Something", priority: 0)
and after retrieve objects:
var tasks: Results<Task>!
viewdidLoad(){
let realm = try! Realm()
tasks = realm.objects(Task.self).sorted(byKeyPath: "priority")
}
or you can make date like this:
class Task: Item {
dynamic var something = ""
dynamic var created = Date()
override class func indexedPriority() -> [String]{
return ["created"]
}
}
add to realm:
let task = Task(something: "Something")
and after retrieve objects:
var tasks: Results<Task>!
viewdidLoad(){
let realm = try! Realm()
tasks = realm.objects(Task.self).sorted(byKeyPath: "created")
}
first one you must update yourself to assign priority, second one will be automatically updated
Related
After reading the Swift documentation and various online tutorials, it still a little hard to wrap my head around reference types versus value types.
I'm following a tutorial from a Swift TDD book to understand OOP architecture in Swift. The tutorial is based on creating a to do list app. In the beginning of the book we created a struct to resemble each to do item. Next we created an class called ItemManger to manage the array that holds to do items and another array to hold checked off to do items. I can understand the idea of to do items being made from a struct because its a value type which creates a new instance every time its instantiated and the itemManager being made from a class since we only need one itemManger to keep track of to do items. The question that I've come up with is when we create an instance of the ItemManager type (which is a class) inside another class or view controller, will this refer to the same class we created before in which we'll be able to access the to do item arrays?
Before this book, I assumed in order to keep track of variables from a class, we have to mark them as static.
Here is the itemManager class:
import Foundation
class ItemManager {
var toDoCount: Int {return toDoItems.count }
var doneCount: Int {return doneItems.count }
private var toDoItems: [ToDoItem] = []
private var doneItems: [ToDoItem] = []
func add(item: ToDoItem) {
if !toDoItems.contains(item){
toDoItems.append(item)
}
}
func checkItem(at index: Int) {
let item = toDoItems.remove(at: index)
doneItems.append(item)
}
func doneItem(at index: Int) -> ToDoItem{
return doneItems[index]
}
func item(at index: Int) -> ToDoItem{
return toDoItems[index]
}
func removeAll(){
toDoItems.removeAll()
doneItems.removeAll()
}
}
Here is another class where we create an instance variable of the ItemManager type:
import UIKit
enum Section: Int {
case toDo
case done
}
class ItemListDataProvider: NSObject, UITableViewDataSource, UITableViewDelegate {
var itemManager: ItemManager?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let itemManager = itemManager else{return 0}
guard let itemSection = Section(rawValue: section)else{ fatalError() }
let numberOfRows: Int
switch itemSection {
case .toDo:
numberOfRows = itemManager.toDoCount
case .done:
numberOfRows = itemManager.doneCount
}
return numberOfRows
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell
guard let itemManager = itemManager else { fatalError() }
guard let section = Section(rawValue: indexPath.section) else { fatalError() }
let item: ToDoItem
switch section {
case .toDo:
item = itemManager.item(at: indexPath.row)
case .done:
item = itemManager.doneItem(at: indexPath.row)
}
cell.configCell(with: item)
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
}
In the code you posted you are not creating an instance of ItemManager.
Here is another class where we create an instance variable of the ItemManager type:
ItemListDataProvider can have a ItemManager but it does not create one. Creating an instance of a class works by calling it's constructor like this:
// Creates an instance of ItemManager and assigns it to itemManager
let itemManager = ItemManager()
Because you did not show where your item manager is created, the question
will this refer to the same class we created before in which we'll be able to access the to do item arrays?
can not really be answered. Where did you create an instance of ItemManager and what did you do with it?
Here is an example:
let itemManagerA = ItemManager()
let itemListDataProviderA() = ItemListDataProvider()
itemListDataProviderA.itemManager = itemManagerA
let itemListDataProviderB() = ItemListDataProvider()
itemListDataProviderB.itemManager = itemManagerA
In this example both ItemListProviders have the same ItemManager and thus have access to the same item arrays.
On the contrary if you are doing something like this:
let itemManagerA = ItemManager()
let itemListDataProviderA() = ItemListDataProvider()
itemListDataProviderA.itemManager = itemManagerA
let itemManagerB = ItemManager() // <-- This creates a SECOND instance of ItemManager
let itemListDataProviderB() = ItemListDataProvider()
itemListDataProviderB.itemManager = itemManagerB // <-- We use the SECOND instance instead of the first one for itemListDataProviderB
both ItemListProviders have different instances of ItemListProvider and do not have access to the same items.
I'm using Realm for my Note-taking-app.
I used core data before and I am now migrating core data to realm, but i got trouble! reordering objects like this causes error.
do {
let realm = try Realm()
let source = myNote[sour!.row]
try realm.write() {
myNote.remove(objectAtIndex: sourceIndexPath!.row)
myNote.insert(source, at: destensionIndexPath!.row)
}
}
catch {
print("handle error")
}
So I added
orderPosition property to my object
dynamic var orderPosition: Int = 0
and changed tableView moveRowAtIndexPath to this
ReorderingRealmResultsInTableView.swift
but it didn't helped a lot.
so How can I reorder objects in realm?
I'd encourage you to store ordered items in a List rather than sorting based on an orderPosition property.
Storing the index manually will be much less performant when moving an item, because all objects between the "old index" and "new index" will need to be mutated to account for the change.
You can then use List.move(from:to:) to move an object from one index to another, which should correspond directly to the indices in the table view you're reordering.
Here's a tutorial you can follow guides you through building a task management app, including support for reordering tasks: https://realm.io/docs/realm-mobile-platform/example-app/cocoa/
A List is certainly efficient and clean, though I wasn't sure how I would sync it through a server. So in my case I'm using orderPosition: Double, and the value is calculated as the middle of the two existing orderPositions that the object is inserted between. Also keep in mind that you can perform the write without updating the tableView from the notification: try! list.realm?.commitWrite(withoutNotifying: [notificationToken!]).
As others suggested, List is the solution. Here is the example to implement the approach on Swift 5:
import UIKit
import RealmSwift
// The master list of `Item`s stored in realm
class Items: Object {
#objc dynamic var id: Int = 0
let items = List<Item>()
override static func primaryKey() -> String? {
return "id"
}
}
class Item: Object {
#objc dynamic var id: String = UUID().uuidString
#objc dynamic var name = ""
}
class ViewController: UITableViewController {
let realm = try! Realm()
var items = RealmSwift.List<Item>()
override func viewDidLoad() {
super.viewDidLoad()
// initialize database
var itemsData = realm.object(ofType: Items.self, forPrimaryKey: 0)
if itemsData == nil {
itemsData = try! realm.write { realm.create(Items.self, value: []) }
}
items = itemsData!.items
// temporarily add new items
let newItem1 = Item()
newItem1.name = "Item 1"
let newItem2 = Item()
newItem2.name = "Item 2"
try! realm.write {
items.append(newItem1)
items.append(newItem2)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
...
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
try! items.realm?.write {
items.move(from: sourceIndexPath.row, to: destinationIndexPath.row)
}
}
}
You can find example applications for both iOS and macOS in GitHub Repo (https://github.com/realm/realm-cocoa) under examples/, demonstrating how to use many features of Realm like migrations, how to use it with UITableViewControllers, encryption, command-line tools and much more.
I need to take variables that are populated in the viewDidLoad method to show up on labels connected to a custom cell. What i am trying to do is:
Find out SKUs in user's box stored in database
Use SKU to find out details of the product stored in database
Store product details in appropriate variable
Take said variable and populate labels in a custom table cell
The issue is that I can store the variable in the viewDidLoad method, but when I try to call the variable to populate the custom table cell, the variable is blank.
I am using Firebase to store the data. The fire base nodes are set up as the following, Node 1: Products/Sku/Item details Node 2: Box/UID/Skus
"products" : {
"0123456" : {
"brand" : "Nike",
"item_name" : "basketball"
}
},
"box" : {
"jEI5O8*****UID" : {
"sku" : "0123456"
I've been scouring through stack overflow, youtube, google, etc but i can't seem to find a solution...If you can help point me in the right direction that would be greatly appreciated! FYI I am new to swift/firebase.
import UIKit
import FirebaseAuth
import FirebaseDatabase
class drawerFaceExampleViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
var databaseRef = FIRDatabase.database().reference()
var loggedInUser = AnyObject?()
var loggedInUserData = AnyObject?()
var itemDrawer = AnyObject?()
var dataDict = AnyObject?()
#IBOutlet weak var homeTableView: UITableView!
var item_name = String()
var brand_name = String()
override internal func viewDidLoad() {
super.viewDidLoad()
self.loggedInUser = FIRAuth.auth()?.currentUser
//get the logged in users details
self.databaseRef.child("user_profiles").child(self.loggedInUser!.uid).observeSingleEventOfType(.Value) { (snapshot:FIRDataSnapshot) in
//store the logged in users details into the variable
self.loggedInUserData = snapshot
//get all the item sku's that are in the user's box
self.databaseRef.child("box/\(self.loggedInUser!.uid)").observeEventType(.ChildAdded, withBlock: { (snapshot:FIRDataSnapshot) in
let sku = snapshot.value! as! String
//access the 'products' node to extract all the item details
self.databaseRef.child("products").child(sku).observeSingleEventOfType(.Value, withBlock: { (snapshot:FIRDataSnapshot) in
if let itemvariable = snapshot.value!["item"] as? String {
self.item_name = item variable
//testing to see if item name is stored, works!
print("testing=", self.item_name)
}
if let brandvariable = snapshot.value!["brand"] as? String{
self.brand_name = brand variable
//testing to see if brand name is stored, works!
print("testingBrand =", self.brand_name)
}
})
self.homeTableView.insertRowsAtIndexPaths([NSIndexPath(forRow:0,inSection:0)], withRowAnimation: UITableViewRowAnimation.Automatic)
}){(error) in
print(error.localizedDescription)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: drawerFaceExampleTableViewCell = tableView.dequeueReusableCellWithIdentifier("drawerFaceExampleCell", forIndexPath: indexPath) as! drawerFaceExampleTableViewCell
//checking to see the item & brand name has been extracted...but blank :(
print("item_name=",self.item_name)
print("item_name=",self.item_name)
//this is where item & brand name extracted from viewDidLoad to display in the cell.
cell.configure(nil, brandName: brand_name, itemName: item_name)
return cell
}
}
Sounds like your data hasn't finished loading yet when you go to read the variable. You need to update your UI after the download is complete, in the completion handler:
if let itemvariable = snapshot.value!["item"] as? String {
self.item_name = item variable
//testing to see if item name is stored, works!
print("testing=", self.item_name)
}
if let brandvariable = snapshot.value!["brand"] as? String{
self.brand_name = brand variable
//testing to see if brand name is stored, works!
print("testingBrand =", self.brand_name)
}
// Update UI here.
Attached at very bottom of this question is my inventory controller file. My problem is I'm getting duplicate results in all the sections. I narrowed down the reason to
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
My code in that function does not account for how many rows there are in each section. As such I'm just printing out the same duplicate results every section.
The actual question is listed after the images below...
Refer to images below:
I also have the ability to change the index from my settings menu so it can index by numbers, like 0-9. Refer to image below:
That said, I currently load the data from Core Data. Attached is reference image of the entities I use and there relationships.
The Question:
My question is, how can I get the results from coreData to be sorted into the A,B,C type sections or 1,2,3 sections so that navigating the table will be simple.
My hunch is the line that says let inventoryRecords = try moc.executeFetchRequest(inventoryFetchRequest) as? [Inventory] needs a sort descriptor to sort based on how I like, but how I then take the data and put into the correct array structure to split into the sections I need...I have no idea.
globals.swift
import Foundation
import CoreData
//Array of Inventory & Store Core Data Managed Objects
var g_inventoryItems = [Inventory]()
var g_storeList = [Store]()
var g_appSettings = [AppSettings]()
var g_demoMode = false
InventoryController.swift
import UIKit
import CoreData
class InventoryController: UIViewController, UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var inventoryTable: UITableView!
var numberIndex = ["0","1","2","3","4","5","6","7","8","9"]
var letterIndex = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
var moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext //convinience variable to access managed object context
// Start DEMO Related Code
func createInventoryDummyData(number: Int) -> Inventory{
let tempInventory = NSEntityDescription.insertNewObjectForEntityForName("Inventory", inManagedObjectContext: moc) as! Inventory
tempInventory.name = "Test Item # \(number)"
tempInventory.barcode = "00000000\(number)"
tempInventory.currentCount = 0
tempInventory.id = number
tempInventory.imageLargePath = "http://website.tech//uploads/inventory/7d3fe5bfad38a3545e80c73c1453e380.png"
tempInventory.imageSmallPath = "http://website.tech//uploads/inventory/7d3fe5bfad38a3545e80c73c1453e380.png"
tempInventory.addCount = 0
tempInventory.negativeCount = 0
tempInventory.newCount = 0
tempInventory.store_id = 1 //belongs to same store for now
//Select a random store to belong to 0 through 2 since array starts at 0
let aRandomInt = Int.random(0...2)
tempInventory.setValue(g_storeList[aRandomInt], forKey: "store") //assigns inventory to one of the stores we created.
return tempInventory
}
func createStoreDummyData(number:Int) -> Store{
let tempStore = NSEntityDescription.insertNewObjectForEntityForName("Store", inManagedObjectContext: moc) as! Store
tempStore.address = "100\(number) lane, Miami, FL"
tempStore.email = "store\(number)#centraltire.com"
tempStore.id = number
tempStore.lat = 1.00000007
tempStore.lng = 1.00000008
tempStore.name = "Store #\(number)"
tempStore.phone = "123000000\(number)"
return tempStore
}
// End DEMO Related Code
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
print("InventoryController -> ViewDidLoad -> ... starting inits")
//First check to see if we have entities already. There MUST be entities, even if its DEMO data.
let inventoryFetchRequest = NSFetchRequest(entityName: "Inventory")
let storeFetchRequest = NSFetchRequest(entityName: "Store")
do {
let storeRecords = try moc.executeFetchRequest(storeFetchRequest) as? [Store]
if(storeRecords!.count<=0){
g_demoMode = true
print("No store entities found. Demo mode = True. Creating default store entities...")
var store : Store //define variable as Store type
for index in 1...3 {
store = createStoreDummyData(index)
g_storeList.append(store)
}
}
let inventoryRecords = try moc.executeFetchRequest(inventoryFetchRequest) as? [Inventory]
if(inventoryRecords!.count<=0){
g_demoMode = true
print("No entities found for inventory. Demo mode = True. Creating default entities...")
var entity : Inventory //define variable as Inventory type
for index in 1...20 {
entity = createInventoryDummyData(index)
g_inventoryItems.append(entity)
}
print("finished creating entities")
}
}catch{
fatalError("bad things happened \(error)")
}
print("InventoryController -> viewDidload -> ... finished inits!")
}
override func viewWillAppear(animated: Bool) {
print("view appearing")
//When the view appears its important that the table is updated.
//Look at the selected Store & Use the LIST of Inventory Under it.
inventoryTable.reloadData()//this is important to update correctly for changes that might have been made
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
print("inventoryItemControllerPrepareForSegueCalled")
if segue.identifier == "inventoryInfoSegue" {
let vc = segue.destinationViewController as! InventoryItemController
if let cell = sender as? InventoryTableViewCell{
vc.inventoryItem = cell.inventoryItem! //sets the inventory item accordingly, passing its reference along.
}else{
print("sender was something else")
}
}
}
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
//This scrolls to correct section based on title of what was pressed.
return letterIndex.indexOf(title)!
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
//Use correct index on the side based on settings desired.
if(g_appSettings[0].indextype=="letter"){
return letterIndex
}else{
return numberIndex
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//TODO: Need to figure out how many rows for ...column A,B,C or 1,2,3 based on indexType using~
//To do this we need to organize the inventory results into a section'ed array.
if(g_appSettings[0].selectedStore != nil){
return (g_appSettings[0].selectedStore?.inventories!.count)! //number of rows is equal to the selected stores inventories count
}else{
return g_inventoryItems.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("InventoryTableCell", forIndexPath: indexPath) as! InventoryTableViewCell
if(g_appSettings[0].selectedStore != nil){
//Get the current Inventory Item & Set to the cell for reference.
cell.inventoryItem = g_appSettings[0].selectedStore?.inventories?.allObjects[indexPath.row] as! Inventory
}else{
//This only happens for DEMO mode or first time.
cell.inventoryItem = g_inventoryItems[indexPath.row]//create reference to particular inventoryItem this represents.
}
cell.drawCell() //uses passed inventoryItem to draw it's self accordingly.
return cell
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if(g_appSettings[0].indextype == "letter"){
return letterIndex[section]
}else{
return numberIndex[section]
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if(g_appSettings[0].selectedStore != nil){
if(g_appSettings[0].indextype=="letter"){
return letterIndex.count
}else{
return numberIndex.count
}
}else{
return 1//only one section for DEMO mode.
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//dispatch_async(dispatch_get_main_queue()) {
//[unowned self] in
print("didSelectRowAtIndexPath")//does not recognize first time pressed item for some reason?
let selectedCell = self.tableView(tableView, cellForRowAtIndexPath: indexPath) as? InventoryTableViewCell
self.performSegueWithIdentifier("inventoryInfoSegue", sender: selectedCell)
//}
}
#IBAction func BarcodeScanBarItemAction(sender: UIBarButtonItem) {
print("test of baritem")
}
#IBAction func SetStoreBarItemAction(sender: UIBarButtonItem) {
print("change store interface")
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
print("text is changing")
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
print("ended by cancel")
searchBar.text = ""
searchBar.resignFirstResponder()
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
print("ended by search")
searchBar.resignFirstResponder()
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
print("ended by end editing")
searchBar.resignFirstResponder()
}
#IBAction func unwindBackToInventory(segue: UIStoryboardSegue) {
print("unwind attempt")
let barcode = (segue.sourceViewController as? ScannerViewController)?.barcode
searchBar.text = barcode!
print("barcode="+barcode!)
inventoryTable.reloadData()//reload the data to be safe.
}
}
//Extention to INT to create random number in range.
extension Int
{
static func random(range: Range<Int> ) -> Int
{
var offset = 0
if range.startIndex < 0 // allow negative ranges
{
offset = abs(range.startIndex)
}
let mini = UInt32(range.startIndex + offset)
let maxi = UInt32(range.endIndex + offset)
return Int(mini + arc4random_uniform(maxi - mini)) - offset
}
}
Update:: **
So I was looking around and found this article (I implemented it).
https://www.andrewcbancroft.com/2015/03/05/displaying-data-with-nsfetchedresultscontroller-and-swift/
I'm really close now to figuring it out. Only problem is I can get it to auto create the sections, but only on another field, like for example store.name, I can't get it to section it into A,B,C sections or 1,2,3.
This is my code for the fetchedResultsController using the methods described in that article.
//Create fetchedResultsController to handle Inventory Core Data Operations
lazy var fetchedResultsController: NSFetchedResultsController = {
let inventoryFetchRequest = NSFetchRequest(entityName: "Inventory")
let primarySortDescriptor = NSSortDescriptor(key: "name", ascending: true)
let secondarySortDescriptor = NSSortDescriptor(key: "barcode", ascending: true)
inventoryFetchRequest.sortDescriptors = [primarySortDescriptor, secondarySortDescriptor]
let frc = NSFetchedResultsController(
fetchRequest: inventoryFetchRequest,
managedObjectContext: self.moc,
sectionNameKeyPath: "store.name",
cacheName: nil)
frc.delegate = self
return frc
}()
Question is what to put for sectionNameKeyPath: now that will make it section it on A B C and I got this !
Found a stackoverflow post very similar to my issue, but need swift answer.
A-Z Index from NSFetchedResultsController with individual section headers within each letter?
Here is another similar article but all objective-c answers.
NSFetchedResultsController with sections created by first letter of a string
Update::
Found another article I think with my exact issue (How to have a A-Z index with a NSFetchedResultsController)
Ok I figured it out, phew was this confusing and took a lot of research.
Okay, so first thing you have to do is create a transient property on the data model. In my case I called it lettersection. To do this in the entity just create a new attribute and call it lettersection and in graph mode if you select it (double click it), you will see option in inspector for 'transient'. This means it won't be saved to the database and is used more for internal reasons.
You then need to manually set up the variable in the extension area of the model definition. Here is how it looks for me.
import Foundation
import CoreData
extension Inventory {
#NSManaged var addCount: NSNumber?
#NSManaged var barcode: String?
#NSManaged var currentCount: NSNumber?
#NSManaged var id: NSNumber?
#NSManaged var imageLargePath: String?
#NSManaged var imageSmallPath: String?
#NSManaged var name: String?
#NSManaged var negativeCount: NSNumber?
#NSManaged var newCount: NSNumber?
#NSManaged var store_id: NSNumber?
#NSManaged var store: Store?
var lettersection: String? {
let characters = name!.characters.map { String($0) }
return characters[0].uppercaseString
}
}
Once you do this, you simply call this new 'lettersection' with the fetchedResultsController like so...
let frc = NSFetchedResultsController(
fetchRequest: inventoryFetchRequest,
managedObjectContext: self.moc,
sectionNameKeyPath: "lettersection",
cacheName: nil)
and everything will work! It sorts by the name of my inventory items, but groups them by the first letters, for a nice A,B,C type list!
"My question is, how can I get the results from coreData to be sorted into the A,B,C type sections or 1,2,3 sections so that navigating the table will be simple."
Using "Store" as your entity and property "name" to be what you want to sort the records by.
override func viewDidLoad() { super.viewDidLoad()
let fetchRequest = NSFetchRequest()
let entity = NSEntityDescription.entityForName("Store", inManagedObjectContext: managedObjectContext)
fetchRequest.entity = entity
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
do {
let foundObjects = try managedObjectContext.executeFetchRequest(fetchRequest)
locations = foundObjects as! [Location]
} catch {
fatalCoreDataError(error) }
}
You are going to use this function to set the number of sections:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return letterindex.count // if that is how you want to construct sections
}
I learned this from the Ray Wenderlich e-book "iOS Apprentice". From Lesson 3 - MyLocations. Highly recommend this and their e-book book on CoreData.
In my RealmSwift (0.92.3) under Xcode6.3, how would I
// the Realm Object Definition
import RealmSwift
class NameEntry: Object {
dynamic var player = ""
dynamic var gameCompleted = false
dynamic var nrOfFinishedGames = 0
dynamic var date = NSDate()
}
The current tableView finds the number of objects (i.e. currently all objects) like follows:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let cnt = RLM_array?.objects(NameEntry).count {
return Int(cnt)
}
else {
return 0
}
}
First question: How would I find the number of objects that have a date-entry after, let's say, the date of 15.06.2014 ?? (i.e. date-query above a particular date from a RealmSwift-Object - how does that work ?). Or in other words, how would the above method find the number of objects with the needed date-range ??
The successful filling of all Realm-Objects into a tableView looks as follows:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("NameCell") as! PlayersCustomTableViewCell
if let arry = RLM_array {
let entry = arry.objects(NameEntry)[indexPath.row] as NameEntry
cell.playerLabel.text = entry.player
cell.accessoryType = entry.gameCompleted ? .None : .None
return cell
}
else {
cell.textLabel!.text = ""
cell.accessoryType = .None
return cell
}
}
Second question: How would I fill into the tableView only the RealmSwift-Objects that have a particular date (i.e. for example filling only the objects that have again the date above 15.06.2014). Or in other words, how would the above method only fill into the tableView the objects with the needed date-range ??
You can query Realm with dates.
If you want to get objects after a date, use greater-than (>), for dates before, use less-than (<).
Using a predicate with a specific NSDate object will do what you want:
let realm = Realm()
let predicate = NSPredicate(format: "date > %#", specificNSDate)
let results = realm.objects(NameEntry).filter(predicate)
Question 1: For the number of objects, just call count: results.count
Question 2: results is an array of NameEntrys after specificNSDate, get the object at indexPath. Example, let nameEntry = results[indexPath.row]
For creating a specific NSDate object, try this answer: How do I create an NSDate for a specific date?