ios - 使用 Blocks 的后台 performFetchWithCompletionHandler 导致崩溃
<p><p>我有一个应用程序可以成功获取并显示我想添加后台获取的 RSS 提要。我收到:线程 1 EXC_BAD_ACCESS(代码=1,地址=0x10),如下所示。</p>
<p>在应用委托(delegate)中:</p>
<pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//setup background fetch
;
return YES;
</code></pre>
<p>}</p>
<pre><code> //background fetch new RSS Feeds
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"performFetchWithCompletionHandler");
UIStoryboard *mainStoryboard = ;
MasterViewController *navigationController = ;
MasterViewController *viewController = navigationController;
[viewController fetchNewDataWithCompletionHandler:^(UIBackgroundFetchResult result) {
completionHandler(result);
}];
}
</code></pre>
<p>在我的主视图 Controller 中:</p>
<pre><code>@property (nonatomic, copy) void (^completionHandler)(BOOL);
- (void) fetchNewDataWithCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler;
- (void) startParsingWithCompletionHandler:(void (^)(BOOL))completionHandler;
-(void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
self.completionHandler = UIBackgroundFetchResultNewData; //completionHandler;
[self startParsingWithCompletionHandler:^(BOOL success){
if (success) {
completionHandler(UIBackgroundFetchResultNewData);
NSLog(@"completionHandler");
}
else{
NSLog(@"error");
}
}];
</code></pre>
<p>}</p>
<pre><code> - (void) storyIsDone//called when parser completed one rss feed
{
numberOfCompletedStories ++;
if (numberOfCompletedStories == self.rssFeedAddresses.count)
{
//if all the feeds are done cancel time-out timer
;
;
storysAreLoading = NO;
;
[ addObserver: self selector: @selector(reloadRSSfeeds) name: @"ReloadFeeds" object: nil];
canRefresh = YES;
NSLog(@"call back");
self.completionHandler (YES);//crash here:Thread 1 EXC_BAD_ACCESS (code=1, Address=0x10)
}//else not yet complete
}
</code></pre>
<p>我收到的输出是:</p>
<p>执行FetchWithCompletionHandler</p>
<p>回电</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><pre><code>self.completionHandler = UIBackgroundFetchResultNewData;
</code></pre>
<p>不匹配类型。 <code>completionHandler</code> 属于 <code>(void (^)(UIBackgroundFetchResult))</code> 类型,而 <code>UIBackgroundFetchResultNewData</code> 属于 <code>NSUInteger</code> 类型。</p>
<pre><code>typedef enum : NSUInteger {
UIBackgroundFetchResultNewData,
UIBackgroundFetchResultNoData,
UIBackgroundFetchResultFailed
} UIBackgroundFetchResult;
</code></pre>
<p>所以当你调用<code>self.completionHandler(YES)</code>时,<code>self.completionHandler</code>是一个<code>NSUInteger</code>,所以<code>NSUInteger(YES) </code> 没有多大意义。</p></p>
<p style="font-size: 20px;">关于ios - 使用 Blocks 的后台 performFetchWithCompletionHandler 导致崩溃,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/30402990/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/30402990/
</a>
</p>
页:
[1]