Frage

I'm attempting to make my iOS6.0 app back compatible with 5.1. I've turned off the obvious things (e.g. autolayout) but am getting stuck at a strange stage.

My app takes data from an XML source and puts it in a core date structure. On iOS 6 this works perfectly. On iOS 5 it gets stuck here

else if (self.dataStorage.documentState == UIDocumentStateClosed) {
   NSLog(@"THIS FIRES = db on disk but closed");
   [self.dataStorage openWithCompletionHandler:^(BOOL success) {
      NSLog(@"THIS NEVER FIRES");
    }];
}

If I look at self.datastorage it is what I would expect (a closed managed document) fileURL: file://localhost/ ..... /Library/Application%20Support/iPhone%20Simulator/5.1/Applications/E3E9192D-2DFE-4882-9041-00A1DF9E98D6/Documents/Default%20Database documentState: [Closed]

Edit: Actually works fine with iOS 5.0 or 6.0+. My problem is purely with iOS 5.1 run on the iPhone simulator. Could this just be a bug with the simulator? It will not open a closed UIManagedDocument nor create an non-existing file.

Here is the full code for completeness:

- (void)setDataStorage:(UIManagedDocument *)database
{
   if (_dataStorage != database) {
      _dataStorage = database;
      [self useDocument];
    }
}

-(UIManagedDocument*) initialiseDatabase {
  if (!self.dataStorage) {  
    NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"DefaultDatabase"];
    self.dataStorage = [[UIManagedDocument alloc] initWithFileURL:url]; // setter will create this for us on disk
  }
  return self.dataStorage;
}
- (void)useDocument {
  if (![[NSFileManager defaultManager] fileExistsAtPath:[self.dataStorage.fileURL path]]) {
    // does not exist on disk, so create it
    NSLog(@"db not on disk");
    [self.dataStorage saveToURL:self.dataStorage.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
       NSLog(@"Doesn't fire");
      }];
} else if (self.dataStorage.documentState == UIDocumentStateClosed) {
    NSLog(@"db on disk but closed");
    // exists on disk, but we need to open it
    [self.dataStorage openWithCompletionHandler:^(BOOL success) {
        NSLog(@"Doesn't fire");
    }];
} else if (self.dataStorage.documentState == UIDocumentStateNormal) {
    NSLog(@"db on disk and open");
}
}

Thanks

War es hilfreich?

Lösung

Now I've identified the problem in a little more detail it appears that many people have asked this question before. Sadly, there has never been a satisfactory solution. However, it is only an issue/bug with the simulator and shouldn't be a problem for real devices (as confirmed by me testing on a 5.1 iPad).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top