解析xml时ios应用程序崩溃

时间:2012-10-16 18:19:51

标签: ios iphone xml

我有一点问题。我做了一个从xml读取并显示图像的应用程序。该应用程序在模拟器上运行良好,但有时应用程序在iphone 4上崩溃。它可能是什么?我的治疗方法是否有问题,或者是其他地方的问题?代码是吼叫。谢谢

-(void)waiting{
    // replace right bar button 'refresh' with spinner

    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    spinner.center = CGPointMake(self.view.bounds.size.width/2, (self.view.bounds.size.height/2)-50);
    spinner.hidesWhenStopped = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:spinner];
    [spinner startAnimating];
    // how we stop refresh from freezing the main UI thread
    dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
    dispatch_async(downloadQueue, ^{

        // do our long running process here
        [NSThread sleepForTimeInterval:0.2];

        // do any UI stuff on the main UI thread
        dispatch_async(dispatch_get_main_queue(), ^{
            [self start];


            [self picture];
            [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(animate) userInfo:nil repeats:YES];


            [spinner stopAnimating];

        });


    });

    dispatch_release(downloadQueue);

}

-(void)start{
    xmlElementObjects = [[NSMutableArray alloc] init];

    parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.i-store.si/Storage/Images/app/galerija.xml"]];
    [parser setDelegate:self];
    [parser parse];

}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if(![elementName compare:@"PictureInfo"])
    {
        array = [[NSMutableArray alloc] init];

    }

    else if(![elementName compare:@"imageURL"])
    {
        currentAttribute = [NSMutableString string];
    }

    else if(![elementName compare:@"imageTitle"])
    {
        currentAttribute = [NSMutableString string];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

    if(![elementName compare:@"PictureInfo"])
    {
    }

    else if(![elementName compare:@"imageURL"])
    {

        [array addObject:currentAttribute];
        pictures=pictures+1;
        currentAttribute = nil;


    }

    else if(![elementName compare:@"imageTitle"])
    {

    }

    else if(![elementName compare:@"Pictures"])
    {

    }
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if(self.currentAttribute)
    {
        [self.currentAttribute appendString:string];
    }
}

更新: 我尝试了Bugsense,这就是我得到的:

CLASS:
SIGNAL

FILE:
_mh_execute_header +

0libobjc.A.dylib 0x31946fbc objc_msgSend + 15
1Foundation 0x31da161d __NSFireTimer + 144
2CoreFoundation 0x37e3aa63 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 14
3CoreFoundation 0x37e3a6c9 __CFRunLoopDoTimer + 364
4CoreFoundation 0x37e3929f __CFRunLoopRun + 1206
5CoreFoundation 0x37dbc4dd CFRunLoopRunSpecific + 300
6CoreFoundation 0x37dbc3a5 CFRunLoopRunInMode + 104
7GraphicsServices 0x3793afcd GSEventRunModal + 156
8UIKit 0x3524c743 UIApplicationMain + 1090
9My_app 0x0000273b _mh_execute_header + 5947

1 个答案:

答案 0 :(得分:1)

您无法使用GCD在主线程上安排NSTimer。

使用:

[NSTimer timerWithTimeInterval:target:selector:userInfo:repeats:]

..创建计时器并:

[[NSRunLoop mainRunLoop] addTimer:forMode:]

..将其添加到主runLoop。

对于替代方案,请查看此问题的答案:Run repeating NSTimer with GCD?