在另一个线程中/同时解析 XML

发布于 2024-09-13 09:15:38 字数 1139 浏览 3 评论 0原文

我有一个应用程序,它通过 XML 文件获取数据。在解析部分期间,数据被写入/插入到设备上的本地 sqlite3 数据库中。 因此,如果数据已同步,则会出现加载屏幕(在后台执行)并告诉用户数据已同步。

通过以下代码,这工作得很好:

加载屏幕部分:

[self.view performSelectorInBackground:@selector(addSubview:) withObject:self.updateView];
[self.view performSelectorInBackground:@selector(bringSubviewToFront:) withObject:self.updateView];

for (NSURL *url in urlArray)
{
    // Set up a UILabel so the user can see which data is syncronized
    [self.updateLabel performSelectorInBackground:@selector(setText:) withObject:[NSString stringWithFormat:@"%@",url]];

    // Parsing part
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

    // Setting the database-manager as delegate
    [xmlParser setDelegate:self.datencontroller];
    [xmlParser parse];
    [xmlParser release];
    [url release];
}

[self.updateView removeFromSuperview];

现在我遇到了以下问题:显然解析正在阻止正在运行的应用程序。由于我想实现取消方法,以便用户能够中断同步,所以我遇到了问题。

在解析部分完成/完成之前,不会注意到任何操作。因此,如果应用程序没有注意到按下按钮,那么实现调用取消方法的按钮是毫无用处的。

我尝试了多种在后台执行的方法等,但我遇到了数据管理器将数据存储在一个数据库中的问题。所以没有办法同时从两种不同的方式访问它并插入数据。

你们中有人能帮我解决这个问题吗? 提前致谢。

I have got an application, that is getting data via XML-files. During the parsing-part the data is written/inserted to the local sqlite3-database on the device.
So, if the data is syncronized, there does a loading-screen appear (performed in background) and is telling the user, that the data is syncronized.

This is working quite well by the following code:

Loading-screen part:

[self.view performSelectorInBackground:@selector(addSubview:) withObject:self.updateView];
[self.view performSelectorInBackground:@selector(bringSubviewToFront:) withObject:self.updateView];

for (NSURL *url in urlArray)
{
    // Set up a UILabel so the user can see which data is syncronized
    [self.updateLabel performSelectorInBackground:@selector(setText:) withObject:[NSString stringWithFormat:@"%@",url]];

    // Parsing part
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

    // Setting the database-manager as delegate
    [xmlParser setDelegate:self.datencontroller];
    [xmlParser parse];
    [xmlParser release];
    [url release];
}

[self.updateView removeFromSuperview];

Now i got the following problem: obviously the parsing is blocking the operating App. Since I want to implement a cancel-method so the user is able to interrupt the syncronizing, I got a problem.

No action is noticed, until the parsing-part is done/finished. So it's quite useless to implement a button that is calling a cancel-method, if the button-pressing is not noticed by the app.

I tried several ways with performing in background etc. but I got the problem that the data manager is storing the data in one single database. So there is no way to access it simultaneously from two different ways and insert data.

Can anyone of you help me with this?
Thanks in advance.

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

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

发布评论

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

评论(3

卖梦商人 2024-09-20 09:15:38

切勿从后台线程更新 UI。始终在主线程上执行 UI 更新选择器。

Never update the UI from background threads. Always perform UI-updating selectors on the main thread.

分開簡單 2024-09-20 09:15:38

如何停止 NSXMLParser?

此解决方案可能会提供一些指导

How do i stop NSXMLParser?

this solution might provide some guidance

深爱不及久伴 2024-09-20 09:15:38

Sqlite3数据库可以从多个线程访问;您只需要为每个线程打开不同的数据库句柄。

Sqlite3 databases can be accessed from multiple threads; you just need to open a different database handle for each thread.

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