Objective-C - EXC_BAD_ACCESS error with ALAsset and XmlWriter

253 views Asked by At

I'm using the ALAssetLibrary to retrieve information about the images on the device (IOS Simulator) for the moment. Now that I have the needed information, I want to write them like an xml file. To do this I'm using the XmlStreamWriter (https://github.com/skjolber/xswi) simple and easy to use. My problem is that I'm having a EXC_BAD_ACCESS (code=1) error when I run the application. I know that it is related to the streamWriter because if I comment this lines of code the program work perfectly. Here there is my code (I'm using ARC):

XMLWriter* xmlWriter = [[XMLWriter alloc]init];
[xmlWriter writeStartDocumentWithEncodingAndVersion:@"UTF-8" version:@"1.0"];
[xmlWriter writeStartElement:@"Photos"]; 
NSMutableArray *list = [[NSMutableArray alloc] init];
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
   [group setAssetsFilter:[ALAssetsFilter allPhotos]];
   [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
       if (asset){
                [xmlWriter writeStartElement:@"Photos"];
                NSString *description = [asset description];
                NSRange first = [description rangeOfString:@"URLs:"];
                NSRange second = [description rangeOfString:@"?id="];
                NSString *path = [description substringWithRange: NSMakeRange(first.location + first.length, second.location - (first.location + first.length))];
                [xmlWriter writeAttribute:@"id" value:path];
                [xmlWriter writeEndElement:@"Photos"];                    
            }
        }];
    }
} failureBlock:^(NSError *error) {
    NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
[xmlWriter writeEndElement];
[xmlWriter writeEndDocument];
NSString* xml = [xmlWriter toString];
NSLog(@"XML: %@", xml);

Any idea of what can be the problem?

I also have an image with the related error: Error image

Thanks

1

There are 1 answers

1
Wain On

enumerateGroupsWithTypes calls the block to process the found information asynchronously. So, you are calling [xmlWriter writeEndDocument before you have ever actually written any real content into the writer.

You need to change how you complete the write operation so that it is done inside the block and when group is passed as nil. Add an else block to your existing check and put

[xmlWriter writeEndElement];
[xmlWriter writeEndDocument];
NSString* xml = [xmlWriter toString];
NSLog(@"XML: %@", xml);

In it (and whatever you subsequently do).