How come I can't access a file url created with URLWithString:?

I'm storing some objects in Core Data. My NSManagedObject subclass has a urlString property (which I get from the original source url's absoluteString).


When I create or update my NSManagedObjects, I grab the absoluteString for the URL and store it. Now my managed object subclass has a URL property which I compute using the urlString.


-(NSURL*)URL
{
   return [NSURL URLWithString:self.urlString];
}


Now, this works only if the file doesn't have a space. absoluteString gives an escpaed string I believe, so whether or not the file name contains a space shouldn't matter?


When I log out the returned URL, it matches the urlString, but when I try to access anything on the URL, I get an error:

Error Domain=NSCocoaErrorDomain Code=260 "The file “my file.txt” couldn’t be opened because there is no such file."


Now I did a little test with the original URL against the one returned from URL:


NSURL *originalURL = //grabbed from the data used to create the NSManagedObject subclass..

myManagedObject.urlString = originalURL.absoluteString; //<---set the urlString property.

NSURL *computedURLFromAbsoluteString = self.URL;

if ([computedURLFromAbsoluteString isEqual:originalURL])
{
     NSLog(@"Equal."); //<--Logs out.
}


Now, if I use the originalURL....I can access the resource, but if I use the URL returned from the URL method, I get the error even though the URLs are equal.


These files are part of my app's sandboxed container so I see no reason why I should be denied permission (error doesn't say that it is a permissions error either, it says the file doesn't exist) and it also seems quirky that this problem only occurs if the file has a space in its name. Am I missing something? I guess I can try to store bookmark data, but I'd rather not do that for every file when creating these managed objects.

Well, there seems to be some vodoo going on.


I'm populating Core Data with results from NSMetadataQuery, in an attempt to get a normal interface for NSMetadataQuery results. The plan is to feed the query results to Core Data, and just sync my interface up with a NSFetchedResultsController because NSMetadataQuery will make you cry.


The problem seems to be that the system will prevent you from doing this. If I store the URL as a string in Core Data and reconstitute it as a URL (like I did above) and then try to do getPromisedItemResourceValue:forKey:error: it barfs on me with that URL...even though it is the same URL you get from the NSMetadataItem...


This only fails if the file is not downloaded on the device. If the file is already downloaded, it works.


Also, you can't get a bookmark for a file URL if it isn't downloaded from iCloud it seems, so storing that isn't going to work either. So there does not seem to be a reliable way to keep a reference to the Ubiquitous file without downloading the file first...or keeping the URL retrieved from NSMetadataItems in memory. Or am I missing something?

How come I can't access a file url created with URLWithString:?
 
 
Q