簡體   English   中英

遞歸調用塊時為EXC_BAD_ACCESS

[英]EXC_BAD_ACCESS when calling a block recursively

我正在使用新的iOS Spotify SDK。 我需要為我的應用程序獲取用戶保存的所有曲目。 結果是分頁的。 因此,我嘗試遞歸使用requestNextPageWithSession:callback:直到獲取了所有頁面。

首先,我最初要求保存的曲目。 這將成功返回首頁,因此,如果還有其他頁面,我將調用getNextPage()

__block SPTListPage *allPages;
[SPTRequest savedTracksForUserInSession:session callback:^(NSError *error, SPTListPage *firstPage) {
   if (!error) {
      allPages = firstPage;
      if (firstPage.hasNextPage) {
         getNextPage(firstPage);
      }
      else {
         // All tracks were in the first page
      }
   }
}];

getNextPage()被聲明為上面的代碼塊,如下所示:

Block getNextPage;
getNextPage = ^(SPTListPage *page) {
    [page requestNextPageWithSession:session callback:^(NSError *error, SPTListPage *nextPage) {
        if (!error) {
            [allPages pageByAppendingPage:nextPage];
            if (nextPage.hasNextPage) {
                getNextPage(nextPage);
            }
            else {
                // Got all pages
            }
        }
    }];
};

僅供參考-我已將“塊”定義為在全球范圍內使用:

typedef void (^Block)();

問題是我第一次嘗試在該塊內遞歸使用getNextPage()時,該行崩潰並在該行上顯示EXC_BAD_ACCESS。 stacktrace並沒有幫助,但看起來像getNextPage已發布。 希望有人解決了類似的問題。

您必須保存對塊的引用,否則在執行到達作用域末尾時將其清除。 您可以在保存它的類上創建一個屬性。

@property (nonatomic, copy) Block block;

或者,您可以只使用方法。

- (void)fetchNextPage:(SPTListPage *)page {
  [page requestNextPageWithSession:session callback:^(NSError *error, SPTListPage *nextPage) {
    if (!error) {
      [allPages pageByAppendingPage:nextPage];
      if (nextPage.hasNextPage) {
        [self fetchNextPage:nextPage];
      }
      else {
        // Got all pages
      }
    }
  }];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM