Code Structure - Swift

Discussion in 'iOS Programming' started by LukasValine, Dec 7, 2015.

  1. LukasValine, Dec 7, 2015
    Last edited: Dec 7, 2015

    LukasValine macrumors regular


    Nov 5, 2013
    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?
    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> { ... }
  2. grandM macrumors 65816


    Oct 14, 2013
    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.
  3. 1458279 Suspended


    May 1, 2010
    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.
  4. Punkjumper macrumors member

    Jan 12, 2013
    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.

Share This Page