iOS NSOperation 子类在执行时冻结设备旋转

发布于 2024-11-28 13:22:33 字数 1239 浏览 1 评论 0原文

我需要你的帮助。我编写了自己的自定义 NSOperation 类,名为 GetNewsOperation。我这样称呼它:

GetNewsOperation *getNewsOperation = [[GetNewsOperation alloc] initWithLocalNewsCategories:self];
[loadNewsOperationQueue addOperation:getNewsOperation];
[getNewsOperation release];

在 GetNewsOperation 类中,我实现了用于初始化的 init 方法和用于执行操作并将数据返回到主线程的 ma​​in 方法。

主要方法如下所示:

- (void)main {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    AppSettingsController *sharedAppSettingsController = [AppSettingsController sharedAppSettingsController];

    if( [type isEqualToString:@"getCategory"] ) {
        NSMutableArray *parsedData = [[NSMutableArray alloc] initWithArray:[sharedAppSettingsController getLocalNewsCategories]];

        [newsViewController performSelectorOnMainThread:@selector(loadDataResponse:) withObject:[NSArray arrayWithObjects:parsedData, nil] waitUntilDone:NO];

        [parsedData release]; parsedData = nil;
    }

    [pool release];
}

一切正常,但我有一个小问题。调用此操作时,应用程序不会随着设备方向变化而旋转。操作完成后会发生变化。

据我所知,此操作肯定在新线程(而不是主线程)中运行,因为应用程序中的所有其他元素都处于活动状态(未冻结)。但我唯一的问题是方向。如果应用程序仅在发生此操作时不旋转,则该应用程序看起来很糟糕......

我该如何解决这个问题?

谢谢!

I need your help. I have write my own custom NSOperation class called GetNewsOperation. I call it like this:

GetNewsOperation *getNewsOperation = [[GetNewsOperation alloc] initWithLocalNewsCategories:self];
[loadNewsOperationQueue addOperation:getNewsOperation];
[getNewsOperation release];

In GetNewsOperation class I have implemented init method for initialization and main method for executing operation and returning data back to the main thread.

Main method looks like this:

- (void)main {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    AppSettingsController *sharedAppSettingsController = [AppSettingsController sharedAppSettingsController];

    if( [type isEqualToString:@"getCategory"] ) {
        NSMutableArray *parsedData = [[NSMutableArray alloc] initWithArray:[sharedAppSettingsController getLocalNewsCategories]];

        [newsViewController performSelectorOnMainThread:@selector(loadDataResponse:) withObject:[NSArray arrayWithObjects:parsedData, nil] waitUntilDone:NO];

        [parsedData release]; parsedData = nil;
    }

    [pool release];
}

Everything works fine but I have a minor problem. When this operation is called application does not rotate on device orientation change. It changes after operation is finished.

As far as I know this operation is running for sure in new thread (not in main) cos all other elements in the app are active (not freezed). But I have only problem with orientation. The app looks kind a crappy if application does not rotate only when this operation occurs...

How can I solve this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戒ㄋ 2024-12-05 13:22:33

实际上它的工作原理就像预测的那样。我在主线程上做了其他事情,阻塞了应用程序。

Actually it works like predicted. I was doing something else on main thread that blocked application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文