2

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

4

1 回答 1

2

现在我已经更详细地确定了这个问题,似乎很多人以前都问过这个问题。可悲的是,从来没有一个令人满意的解决方案。但是,这只是模拟器的问题/错误,对于真实设备不应该是问题(正如我在 5.1 iPad 上测试所证实的那样)。

于 2013-03-03T19:32:38.167 回答