Basic Fetch Question

One of my tables has 100 rows. Can't be helped, just the nature of the app. When I fetch the data, I need to assign the values in order to fields on the screen. I've created an outlet collection to make this easy and would like to do this in a for loop to minimize the code.


I'm having trouble understanding the nature of the fetch to make this easy..... I understand I'm fetching into an array of the type of the data.


In my example, just to make it clear, I fetch two sorted rows - the first fetched row [0] brings back .tile_1_1, through .tile_10_10 and has the "correct values". The second fetched row [1] brings back the "given values" for all of these tiles and is also the "current values", hence the assignment made in the for loop.


I need to change the .tile_1_1 to work in the for loop, but can't figure out what type of equivalent "index" for the data I'm trying to find - it's not a 3 dimensional array, but I can't quite wrap my head around this. Can someone point me in the right direction, please? Should I be trying to retrieve this into a dictionary first, or some other retrieval method?


It would be something like (for the first code line):

tileButtons[x].correctValue = Int(truncating: fetchingDataTiles[0].index[x]


My alternative is what I have now and haven't included - a massive, manual retrieval and assignment code segment. It's long ang ugly, and while it works and I have it done, I'd like to eliminate it for several reasons including the opportunity for breakage in the future.


I hope my explanation makes sense - and that the question is a basic one that I probabluy should know the answer to but just don't... the solution is probably a simple one too - I hope!


                        for x in 0..<tileButtons.count {
                            // TODO: Fix this for the right fetchedDataTiles data
                            tileButtons[x].correctValue = Int(truncating: fetchedDataTiles[0].tile_1_1!)
                            tileButtons[x].givenValue = Int(truncating: fetchedDataTiles[1].tile_1_1!)
                            tileButtons[x].currentValue = Int(truncating: fetchedDataTiles[1].tile_1_1!)
                        }


Thanks in advance!

Replies

Seen Apple's LazyTableImages: Populating UITableView content asynchronously sample?

Sorry, perhaps more is in order: this is coming from CoreData...:


let dataTilesFetch = NSFetchRequest(entityName: "DataTiles")
dataTilesFetch.predicate = NSPredicate(format: "dataRefNumber == %@", String(fetchedData[0].dataRefNumber))
let dataTileTypeSort = NSSortDescriptor(key:"dataTileType", ascending:true)
dataTilesFetch.sortDescriptors = [dataTileTypeSort]
do {
    let fetchedDataTiles = try context.fetch(dataTilesFetch) as! [DataTiles]
    if fetchedDataTiles.count > 0 {
        for x in 0..<tileButtons.count {
            // TODO: Fix this for the right fetchedData
            tileButtons[x].correctValue = Int(truncating: dataTilesFetch[0].tile_1_1!)
            tileButtons[x].givenValue = Int(truncating: dataTilesFetch[1].tile_1_1!)
            tileButtons[x].currentValue = Int(truncating: dataTilesFetch[1].tile_1_1!)
}


it's the .tile_1_1 references that I'm trying to replace. Perhaps it's retrieving them into a dictionary is the suggestion and parsing them from there? If so, I can look into that and see if I can figure that out - I don't yet have that skill, but I can look into and learn that, if that's the best path...

It’s confusing in your description. You‘ve got a hundred row table, data from tile_1_1 to tile_10_10, “given” and current values. Do those tiles run from _1_1 to _1_10, then _2_1 to _2_10, etc?


You can do two-dimensional data in a database, it’s just usually seen as a bit of overkill. You could define a DataTile entity as

  • Row: int
  • Column: int
  • given value
  • current value

and then specify row, column as the unique index values so you don’t have to worry about collisions and get a better sorted result.

Then you could do your fetch request on DataTile, for dataTypeSort specify row and column as the keys for the sort descriptors, and that’d given you something you could loop over easily. (Especially since you can get the results grouped by row if you want...)


If your row,column data is irregular (some rows have more or fewer columns), the database doesn’t mind.

Having looked at this a bit over the past few days, I've worked through a few conclusions:

  • the async idea wasn't quite the solution because the issue wasn't that I was having a problem pulling the data back, it was a need to have a more effective code loop...
  • the data did run from _1_1 through _1_10, then to _2_1 to _2_10, then _3_1 to _3_10, etc, and I did need all data through _10_10 each time I pulled data, i.e. all data was needed every time, and all rows were filled, although some were filled with 0's... but all were significant.


My solution was to normalize my data structure, and then to create an outlet collection from which I could then write an effective code loop that I could then cycle through with a for statement.I'm still rewriting the insert statements and the fetch's but this method makes for a much more effective and efficient solution to the problem.


I appreciate the help, and the various approaches. I'm sure that I'll need to apply the other solutions at some point in the future as I deal with other aspects of this and other apps, so thank you for the help!

Good luck w/it and pls. keep us posted how you get on if you have the time, thanks.