Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

LukasValine

macrumors regular
Original poster
Nov 5, 2013
158
706
So I'm building a pretty basic sheet music editor in swift. I'm using CoreData to store and update the songs. The add, delete and update methods change both the data in the songs array and add stuff to my CoreData model. I'm wondering if this is a dumb way to structure my code. Should I bypass the songs array and interact with Core Data directly?
Code:
class DataStore {
     var songs: Array<Song> = [Song]()

     init() { songs = loadSongsFromCoreData() }

     func addSong(song: Song) { ... }
     func deleteSongAtIndex(index: Int) { ... }
     func updateSongAtIndex(song: Song, index: Int) { ... }

     func loadSongsFromCoreData() -> Array<Song> { ... }

}
 
Last edited:
So I'm building a pretty sheet music editor in swift. I'm using coreData to store and update the songs. The add, delete and update methods change both the data in the songs array and add stuff to my coreData model. I'm wondering if this is a dumb way to structure my code. Should I bypass the songs array and interact with core data directly?
Code:
class DataStore {
     var songs: Array<Song> = [Song]()
  
     init() { songs = loadSongsFromCoreData() }

     func addSong(song: Song) { ... }
     func deleteSongAtIndex(index: Int) { ... }
     func updateSongAtIndex(song: Song, index: Int) { ... }

     func loadSongsFromCoreData() -> Array<Song> { ... }

}
It is a good question actually. NSFetchedResultsController is used with TableViews. It has a cache function. I am curious too if your way is better or worse performance wise.
 
How do you get from the changed data in the array, back to changing the data in Core Data? If this is not automatic, you'd have to remember all the changes and make them back in Core Data yourself?

Is the reason for the array to speed things up? If so, how fast are the users editing the data.

It shouldn't be that hard to try both and determine the response time for each. If the data is stored in the cloud or somewhere where it might be slow or require more data transfer, then the array might be the best choice.

If the edits are simple, making a second array that stores the changes could really speed things up and wouldn't be too hard to employ.
 
keeping your array and core data in sync can be a pain like karlJay mentioned. I only do it when there is a very specific reason and not as normal practice.
 
  • Like
Reactions: ArtOfWarfare
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.