GeoffCoope GeoffCoope - 1 year ago 63
Objective-C Question

How to import hierarchical folder style data

I have an array of objects that I need to iterate over and find/insert into a core data entity. I just can't get my head around how to dive into albumParentFolders and see if they exist, if not add them to the entity albums.

I know it is some kind of recursive method that drills down into the albumParentFolders but due to how my AlbumObjects are structured cannot find a working solution.

So, my array of objects to import are like this

NSArray AlbumObjects
NSString * albumFolderName
NSString * albumDetailsTitle
NSString * albumDetailsURL
NSArray * albumParentFolders (Array of (NSStrings *) of albumFolderNames used for the level in the tree this album lives in. Can be nil for a root folder)

I need to first search core data and if not found insert the albumFolderName into a Core Data Entity called albums. Then using the auto generated class for the album entity, insert a new details object for that folder. e.g. [newFolder addDetailsObject:newDetails] Also, albumFolderName is not unique in core data as the albumFolderName could be listed multiple times but in different parent folders.

Entity: albums
Attribute: folderName
Relationship: albumDetails <------>> Entity : details (For adding multiple albumDetailsTitle and albumDetailsURL)
Relationship: parent <<-------
Relationship: subGroups |
^ |

The album folders are eventually displayed in a treeview using NSOutlineView and IB bindings. This part already works.

If anybody could advise on how to iterate the AlbumObjects, specifically drilling into albumParentFolders so that the entire folder structure can be stored in the entity I would be hugely grateful.

SDK needs to support OSX 10.7+

Answer Source

Start at the root, follow the tree and create missing branches.

Pseudo code:

album = root
if (albumParentFolders != nil) {
    parentAlbum = root
    for (folder in albumParentFolders) {
        album = find folder in parentAlbum.subGroups
        if (album not found) {
            album = insert new album
            album.folderName = folder
            album.parent = parentAlbum
        parentAlbum = album
add details to album
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download