iOS:在 viewDidLoad 中运行数据库查询只能运行一次
所以让我们看看我是否可以对我的问题写出足够清晰的描述...
我正在为博物馆开发一个应用程序,用户可以通过 ID 或扫描二维码标签来查找艺术品... 输入 ID 或扫描标签后,应用程序会将用户从搜索视图发送到信息视图。 信息视图从 SQLite 数据库收集有关艺术品的信息...
我的问题是,在信息视图中,我从数据库类中调用该函数,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paintingInfo = [[PaintingDatabase database] findPaintingByID];
(additional code)
}
并且我获取信息没有问题...工作正常。 .. 但我的问题是,如果我返回搜索视图并输入新的 ID 或扫描新标签,则调用/搜索不会再次运行,因为视图仍在内存中......
那么,我该怎么办 就开始奔跑
NSArray *paintingInfo = [[PaintingDatabase database] findPaintingByID];
每次进入视野
……?我尝试将其放在 viewDidAppear 中,但出现 EXC_BAD_ACCESS 错误...
So let's see if I can write a clear enough description of my problem...
I'm developing an application for a museum, where the user can find artworks either through an id or by scanning a qr tag...
Once either an id is entered or a tag scanned, the application sends the user from the search view, to the info view.
The info view gathers information about the artwork from an SQLite database...
My problem is, that in the info view, I call the function from the database class as such:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paintingInfo = [[PaintingDatabase database] findPaintingByID];
(additional code)
}
And I have no problem getting the info... works fine...
But my problem is, that if I go back to the search view and enter a new id or scan a new tag, the call/search isn't run again, since the view is still in memory...
So, how would I go about running
NSArray *paintingInfo = [[PaintingDatabase database] findPaintingByID];
every time I enter the view...?
I've tried placing it in viewDidAppear instead, but I get an EXC_BAD_ACCESS error...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你已经接近回答你自己的问题了。由于 viewDidLoad 仅在视图加载时被调用,因此如果您使用相同的 ViewController,您将无法获得所需的结果。一种选择可能是每次销毁并创建一个新的 ViewController。从性能角度来看,这可能是可以接受的。另一个选择(您似乎已经探索过)是将代码放在 viewWillAppear 中。我可能会对此进行更多研究,并找出导致崩溃的原因。
I think you are close to answering your own question. Since viewDidLoad only gets called when your view loads, if you are using the same ViewController you will not get the results you are looking for. One option may be to destroy and create a new ViewController each time. This probably would be acceptable, performance-wise. Another option (that you seem to have explored) is to put your code in viewWillAppear. I would probably look into this more, and figure out what is causing your crash.
从简短的描述中很难看出,但在我看来,这更像是一个应用程序架构问题,而不是特定功能的问题。
您可能最好采用替代方法,从信息视图外部运行查询,然后通过委托方法更新视图的属性。这更像是一种 MVC 方法 - 控制器从模型中检索数据,然后将数据传递到要显示的视图。
正如您所描述的,您的信息视图似乎同时采用视图和控制器功能 - 这可能就是为什么您在初始视图完成后尝试获取不同数据时遇到问题的原因。
不过,这次事故听起来不像是一个视图问题——我会附议追踪这个问题的建议,并将其作为一个单独的问题解决。
It's a bit difficult to tell from a brief description, but this feels to me like it's more of an application architecture issue than a problem with a specific bit of functionality.
You might be better off with an alternative approach, running the query from outside the info view, and then update the properties of the view through a delegate method. That's more of an MVC approach - the controller retrieves the data from the model, then passes the data over to the view to be displayed.
As you've described it, it seems like your info view is taking both view and controller functions - which could be why you're running into problems trying to get different data once the initial view is finished with.
The crash doesn't sound like a problem with the view, though - I'd second the advice to track that down and nail it as a separate issue.