The goal is to provide the user with a *.log file using a UIDocument. The class is able to write to the file (if not first saved) but will not initially save it.
When saveToURL is called, it produces an EXC_BREAKPOINT (code=1 subcode=0x104a7b84c). I looked at several other implementations and they all appear to be doing the same thing. None of the objects are zombies, nothing else is overwritten and this is running on a physical iOS 12 device. What am I missing?
@implementation ReceiverLog
- (instancetype)initWithUUID:(NSUUID*)uuid
{
NSString *fileName = [[NSString alloc] initWithFormat:@"receiver-%@.log", uuid.UUIDString];
NSURL *documentsURL = [NSFileManager.defaultManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsURL URLByAppendingPathComponent:fileName];
self = [super initWithFileURL:fileURL];
return self;
}
- (void)open
{
NSURL *fileURL = [self fileURL];
if ([NSFileManager.defaultManager fileExistsAtPath:fileURL.absoluteString]) {
// Never runs
// Open
[self openWithCompletionHandler:^(BOOL success) {
}];
} else {
// Results in EXC_BREAKPOINT
// Create
[self saveToURL:fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
}];
}
}
- (void)close
{
[self closeWithCompletionHandler:^(BOOL success) {
}];
}
- (void)write:(NSString*)format, ...
{
// This works
va_list args;
va_start(args, format);
[_entries addObject:[[NSString alloc] initWithFormat:format arguments:args]];
va_end(args);
[self updateChangeCount:UIDocumentChangeDone];
}
- (id)contentsForType:(NSString *)typeName
error:(NSError * _Nullable *)outError
{
NSString *contentsString = [_entries componentsJoinedByString:@"\n"];
return [contentsString dataUsingEncoding:NSUTF8StringEncoding];
}
- (BOOL)loadFromContents:(id)contents
ofType:(NSString *)typeName
error:(NSError * _Nullable *)outError
{
if (![contents isKindOfClass:[NSData class]]) return NO;
NSString *contentsString = [[NSString alloc] initWithData:contents encoding:NSUTF8StringEncoding];
[_entries removeAllObjects];
[_entries addObjectsFromArray:[contentsString componentsSeparatedByString: @"\n"]];
return YES;
}
@end