Apple提供this of background execution:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:@"MyTask"
expirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
这个例子从来没有对我有任何意义,我已经看到它复制到许多背景应用程序示例中。
第一件没有意义的是expirationHandler中的这两行:
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
似乎bgTask在块中捕获时不会有值。编译器就这样抱怨。然后在dispatch_async下面,示例显示相同的两行。我希望它在dispatch_async中,但不在块中。谁能解释为什么我们在这个区块中有这些线?
每个beginBackgroundTaskWithName的文档也说"标记新的长时间运行的后台任务的开始。"这究竟是怎么做到的?什么定义了任务?是块范围内的任何代码吗?
答案 0 :(得分:4)
printf("\nGetting Card IDs!\n");
// Jmd: Okay, this loop is going to fill up the IDs[] array which has
// 612,967 slots. as this loops through and find new combinations it
// adds them to the end. I need this list to be stable when I set the
// handranks (next set) (I do the insertion sort on new IDs these)
// so I had to get the IDs first and then set the handranks
for (IDnum = 0; IDs[IDnum] || IDnum == 0; IDnum++) {
// start at 1 so I have a zero catching entry (just in case)
for (card = 1; card < 53; card++) {
// the ids above contain cards upto the current card. Now add a new card
ID = MakeID(IDs[IDnum], card); // get the new ID for it
// and save it in the list if I am not on the 7th card
if (numcards < 7) holdid = SaveID(ID);
}
printf("\rID - %d", IDnum); // show progress -- this counts up to 612976
}
// main()
printf("\nSetting HandRanks!\n");
// this is as above, but will not add anything to the ID list, so it is stable
for (IDnum = 0; IDs[IDnum] || IDnum == 0; IDnum++) {
// start at 1 so I have a zero catching entry (just in case)
for (card = 1; card < 53; card++) {
ID = MakeID(IDs[IDnum], card);
if (numcards < 7) {
// when in the index mode (< 7 cards) get the id to save
IDslot = SaveID(ID) * 53 + 53;
} else {
// if I am at the 7th card, get the equivalence class ("hand rank") to save
IDslot = DoEval(ID);
}
maxHR = IDnum * 53 + card + 53; // find where to put it
HR[maxHR] = IDslot; // and save the pointer to the next card or the handrank
}
if (numcards == 6 || numcards == 7) {
// an extra, If you want to know what the handrank when there is 5 or 6 cards
// you can just do HR[u3] or HR[u4] from below code for Handrank of the 5 or
// 6 card hand
// this puts the above handrank into the array
HR[IDnum * 53 + 53] = DoEval(IDs[IDnum]);
}
printf("\rID - %d", IDnum); // show the progress -- counts to 612976 again
}
告诉iOS您的应用程序正在启动新的后台任务。 iOS并不关心哪些代码包含任务,它只知道它需要为您的应用提供更多时间在后台执行。
执行此行后,bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:]
将包含新的后台任务标识符。
当您的后台任务完成后,您可以调用bgTask
并且iOS知道您的应用已完成指定的后台任务,并且可能不再需要任何后台执行时间(您可能还有其他后台任务由{ {1}}杰出的。)
该行:
[application endBackgroundTask:bgTask];
只是家务;如果省略此行,则不会发生任何错误,但beginBackgroundTaskWithName:expirationHandler
将包含无效的标识符。
如果您在应用的后台时间到期之前没有拨打bgTask = UIBackgroundTaskInvalid;
,那么将调用到期处理程序块。
在到期处理程序中bgTask
将具有您在调用endBackgroundTask
时分配的值,因此这是传递给bgTask
并再次分配beginBackgroundTaskWithName:expirationHandler
的内容持家