当应用程序进入前台时重新加载应用程序数据?

发布于 2024-09-28 03:30:31 字数 323 浏览 2 评论 0原文

我是 iPhone 开发新手。我正在构建一个应用程序,该应用程序从本地 sqlite3 数据库加载数据,

-   (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   

当我点击 iPhone 按钮并将其放在后台时,然后我回想起我看到(正常)该应用程序的方式与我离开它时的方式相同。我想做的是,当它进入前台时,重新加载数据,就像从头开始调用一样。

正确的做法是什么?
提前致谢。

I'm new to iPhone development. I'm building an app that loads data from a local sqlite3 DB in

-   (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   

When I tap the iPhone button and put it in the background and then I recall it I see (as normal) the app in the same way I left it. What I would like to do is, when it comes in the foreground, to reload data as if it was called from scratch.

Which is the correct way to do it?
Thanks in advance.

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

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

发布评论

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

评论(4

善良天后 2024-10-05 03:30:31

所以在App Delegate类中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
只有在第一次进入应用程序时才会被调用。然后它会调用- (void)applicationDidBecomeActive:(UIApplication *)application

如果您的 iPhone iOS 是 4.0 或更高版本,当用户单击主页按钮时,它会首先调用 - (void)applicationWillResignActive:(UIApplication *)application,然后调用 - (void)applicationDidEnterBackground :(UIApplication *)应用程序

然后应用程序将在后台运行,直到用户杀死它。当用户再次进入应用程序时,会首先调用 - (void)applicationWillEnterForeground:(UIApplication *)application,然后调用 - (void)applicationDidBecomeActive:(UIApplication *)application

因此,与您的问题相关,您应该调用 applicationWillEnterForeground:applicationDidBecomeActive: 来重新加载数据。尽管在 xcode 对这些方法的注释中,Apple 建议使用 applicationDidBecomeActive: 来重新启动暂停的任务和/或更新用户界面;而在 applicationWillEnterForeground: 中,您可以撤消进入后台时所做的更改。


因此,为了更容易查看,我为每个方法添加了数字标签。这里是当方法被调用时。

0 application:(UIApplication *)application didFinishLaunchingWithOptions: 
1 applicationDidBecomeActive: 
2 applicationWillResignActive: 
3 applicationDidEnterBackground: 
4 applicationWillEnterForeground: 
  • 首次进入应用程序:先调用0,再调用1;

  • 按主页按钮:呼叫 2,然后呼叫 3;

  • 双击主页按钮(多任务处理):呼叫 2;

    • 如果用户选择另一个应用程序或再次点击主页按钮:调用3;

    • 如果用户再次双击主页按钮:调用 1;

  • 再次进入应用程序:调用4,然后调用1;

So in the App Delegate class
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
will only be called at the first time you enter the app.Then it will call - (void)applicationDidBecomeActive:(UIApplication *)application.

If your iphone iOS is 4.0 or later, when a user click on the home button, it will first invoke - (void)applicationWillResignActive:(UIApplication *)application, then - (void)applicationDidEnterBackground:(UIApplication *)application.

Then the app will run in background till the user kill it. When the user enter the app again, it will first invoke - (void)applicationWillEnterForeground:(UIApplication *)application, then - (void)applicationDidBecomeActive:(UIApplication *)application.

So related to your question, you should call either applicationWillEnterForeground: or applicationDidBecomeActive: to reload your data. Though in the xcode's comment of these methods, Apple suggests use applicationDidBecomeActive: to restart paused tasks and/or update user interface; while in the applicationWillEnterForeground:, you can undo the changes you've made in entering the background.


So to make it easier to see, i put a number tag to each method. Here's when the method to be called.

0 application:(UIApplication *)application didFinishLaunchingWithOptions: 
1 applicationDidBecomeActive: 
2 applicationWillResignActive: 
3 applicationDidEnterBackground: 
4 applicationWillEnterForeground: 
  • First enter the app: call 0, then 1;

  • Hit home button: call 2, then 3;

  • Double hit home button(multitasking): call 2;

    • if user choose another app or hit home button again: call 3;

    • if user double hit home button again: call 1;

  • Enter app again: call 4, then 1;

只为守护你 2024-10-05 03:30:31
- (void)applicationDidBecomeActive:(UIApplication *)application {
}

在应用程序委托的上述函数中重新加载数据,以便在应用程序进入前台时刷新数据。

- (void)applicationDidBecomeActive:(UIApplication *)application {
}

Reload the data in the above function of the app delegate to refresh the data when ever the app comes to foreground.

十年九夏 2024-10-05 03:30:31

当 iOS 通知应用程序应该在后台运行时,您可以 exit() 您的应用程序,如下所示:

- (void)applicationDidEnterBackground:(UIApplication *)application {
  exit(0);
}

You can exit() your application when iOS notifies it that it should run in the background like so:

- (void)applicationDidEnterBackground:(UIApplication *)application {
  exit(0);
}
牵你手 2024-10-05 03:30:31

我认为这是 UIApplication 的委托:

- (void)applicationWillEnterForeground:(UIApplication *)application

在iOS 4.0及更高版本中,该方法是
作为过渡的一部分调用
活动状态的背景。
您可以使用此方法撤消许多
您对您所做的更改
进入后申请
背景。对该方法的调用是
随后总是调用
applicationDidBecomeActive:方法,
然后将应用程序从
从非活动状态到活动状态。

I think that is the delegate of UIApplication:

- (void)applicationWillEnterForeground:(UIApplication *)application

In iOS 4.0 and later, this method is
called as part of the transition from
the background to the active state.
You can use this method to undo many
of the changes you made to your
application upon entering the
background. The call to this method is
invariably followed by a call to the
applicationDidBecomeActive: method,
which then moves the application from
the inactive to the active state.

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