0

I have following problem:

I have entities, say person. For each person you can make one photo. I save this path of this photo in an attribute of the person.

Everytime when I open the person, I get the path of the photo, load it into UIImage and everything is ok.

But when I update the app, then the identifier of the app will change, so no photos will be shown anymore. The full path of the photo is wrong after the update.

Is there anyway to prevent the identifier changing?

OR

How can I save the photo with relative paths? Idea: I save the reltive path to coredata and at reading I put it toghether with the app-path.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Philipp Schaller
  • 367
  • 1
  • 5
  • 12
  • This question is unclear. What makes you think that updating the app will change the identifier. And if you use the correct NSFileManager methods as you are supposed to, you will be using the app's sandbox anyway. – Abizern Nov 22 '13 at 10:15

2 Answers2

1

Store the path of the photo as a path relative to the application documents directory. For example, something like "photos/filename.png".

When retrieving the photos, first get the documents directory path and then append the path name with urlByAppendingPathComponent:.

Of course, like with any app, when you do anything to create a new app directory, e.g.

  • changing the app signature
  • different device
  • different simulator,
  • different development computer,
  • etc.

both your core data store and your documents directory will be empty.

You can pre-seed core data and the documents folder with data that you store in the bundle (you copy it over to the documents folder on first run). But this will be the only data you will have available when any of the above conditions create a new instance of your app.

If you want to share states of the data store across devices, maybe you want to consider iCloud. However, iCloud will not work too well with large media files such as photos, and there are also known and yet unresolved problems with iCloud and Core Data.

Community
  • 1
  • 1
Mundi
  • 79,884
  • 17
  • 117
  • 140
0

Store the file path in an NSURL. Then, you can save the bookmark data persistently in core data and retrieve it later.

- (NSData*)bookmarkForURL:(NSURL*)url {
    NSError* theError = nil;
    NSData* bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationSuitableForBookmarkFile
                                            includingResourceValuesForKeys:nil
                                            relativeToURL:nil
                                            error:&theError];
    if (theError || (bookmark == nil)) {
        // Handle any errors.
        return nil;
    }
    return bookmark;
}

- (NSURL*)urlForBookmark:(NSData*)bookmark {
    BOOL bookmarkIsStale = NO;
    NSError* theError = nil;
    NSURL* bookmarkURL = [NSURL URLByResolvingBookmarkData:bookmark
                                            options:NSURLBookmarkResolutionWithoutUI
                                            relativeToURL:nil
                                            bookmarkDataIsStale:&bookmarkIsStale
                                            error:&theError];

    if (bookmarkIsStale || (theError != nil)) {
        // Handle any errors
        return nil;
    }
    return bookmarkURL;
}

https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW10

John K
  • 859
  • 1
  • 8
  • 16