应用程序启动缓慢

发布于 2024-09-26 23:01:26 字数 808 浏览 3 评论 0原文

我的 iPhone 应用程序启动速度非常慢,我不知道为什么。我的 application:didFinishLaunchingWithOptions: 并不是很重,我只是为选项卡栏控制器的五个视图控制器中的每一个设置了 ManagedObjectContext 。

有人知道是什么原因导致启动缓慢吗?谢谢。

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

    mathRootViewController.managedObjectContext = self.managedObjectContext;
    favoriteRootViewController.managedObjectContext = self.managedObjectContext;
    chemistryRootViewController.managedObjectContext = self.managedObjectContext;
    physicsRootViewController.managedObjectContext = self.managedObjectContext;
    shareRootViewController.managedObjectContext = self.managedObjectContext;

    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];


    return YES;
}

My iphone app is launching very slowly, and I have no idea why. My application:didFinishLaunchingWithOptions: isn't really heavy, I'm just setting the managedObjectContext for each of my five view controllers of my tab bar controller.

Does anybody have an idea what causes the slow launch? Thanks.

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

    mathRootViewController.managedObjectContext = self.managedObjectContext;
    favoriteRootViewController.managedObjectContext = self.managedObjectContext;
    chemistryRootViewController.managedObjectContext = self.managedObjectContext;
    physicsRootViewController.managedObjectContext = self.managedObjectContext;
    shareRootViewController.managedObjectContext = self.managedObjectContext;

    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];


    return YES;
}

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

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

发布评论

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

评论(7

烟─花易冷 2024-10-03 23:01:27

在应用慢速目标的运行阶段启动应用程序,并将 DYLD_PRINT_STATISTICS 环境变量设置为 1,以打印出启动过程中缓慢的情况。打印输出将类似于:

    Total pre-main time: 2.2 seconds (100.0%)
     dylib loading time: 1.4 seconds (64.5%)
    rebase/binding time: 205.99 milliseconds (9.2%)
        ObjC setup time:  84.90 milliseconds (3.8%)
       initializer time: 496.64 milliseconds (22.3%)
       slowest intializers :
         libSystem.B.dylib :  21.82 milliseconds (0.9%)
      libglInterpose.dylib : 156.49 milliseconds (7.0%)
     libMTLInterpose.dylib :  47.01 milliseconds (2.1%)
                   AppName : 224.40 milliseconds (10.0%)

Apple 建议 400 毫秒。

来源:使用你的面包

Launch the application with the DYLD_PRINT_STATISTICS environment variable set to 1 on the run phase of your app slow target to get a print out of what's slow in the start up process. The print out will look something like:

    Total pre-main time: 2.2 seconds (100.0%)
     dylib loading time: 1.4 seconds (64.5%)
    rebase/binding time: 205.99 milliseconds (9.2%)
        ObjC setup time:  84.90 milliseconds (3.8%)
       initializer time: 496.64 milliseconds (22.3%)
       slowest intializers :
         libSystem.B.dylib :  21.82 milliseconds (0.9%)
      libglInterpose.dylib : 156.49 milliseconds (7.0%)
     libMTLInterpose.dylib :  47.01 milliseconds (2.1%)
                   AppName : 224.40 milliseconds (10.0%)

Apple recommends 400 milliseconds.

Source: Use Your Loaf

无敌元气妹 2024-10-03 23:01:26

看起来你有一个非常大的初始 xib 文件,在启动时读取和解析该文件以填充 mathRootViewController 等。

尝试等到需要控制器后再加载它们,即将它们放入单独的 xib 文件中并添加看起来有点像这样的方法

- (UIViewController *)mathRootViewController {
    if (nil === mathRootViewController) {
        mathViewController = [[MathViewController alloc] initWithNibName:@"MathViewController" bundle:nil];
        [mathViewController setManagedObjectContext:[self managedObjectContext]];
    }
    return mathRootViewController;
}

,每次使用控制器时,不要只使用 mathRootViewController ;,而是使用 [self mathRootViewController ] - 此模式将等到您第一次需要视图控制器来创建它。

It looks like you have a very large initial xib file that's read and parsed on startup to populate mathRootViewController etc.

Try waiting until you controllers are needed before loading them i.e. put them in a seperate xib file and add methods that look a little like this

- (UIViewController *)mathRootViewController {
    if (nil === mathRootViewController) {
        mathViewController = [[MathViewController alloc] initWithNibName:@"MathViewController" bundle:nil];
        [mathViewController setManagedObjectContext:[self managedObjectContext]];
    }
    return mathRootViewController;
}

and each time you use the controller don't just use mathRootViewController ;, use [self mathRootViewController ] instead - this pattern will wait until the first time you need the view controller to create it.

盗心人 2024-10-03 23:01:26

您是否通过 Xcode 在 iPhone 上运行该应用程序?以这种方式运行时,应用程序往往启动速度非常慢。尝试在 iPhone 本身上启动该应用程序,而不使用 Xcode。

Are you running the app on your iPhone through Xcode? Apps tend to launch very slowly when run that way. Try launching the app on the iPhone itself, without using Xcode.

七七 2024-10-03 23:01:26

首先,我会确认您的假设,即这确实是一个缓慢的函数 - 使用分析器 - 仪器 - CPU 采样器 - 查看该函数显示的计时信息并将其与其他函数进行比较,因为其他东西可能会减慢速度。

一旦您确认了您的假设并且需要更多详细信息,您可以使用“mach_absolute_time”添加非常细粒度的计时。最后用 NSlog 报告时间差异。不要进行过多的日志记录,因为这也会损害性能。

First I would confirm your assumption that this really is the function that is slow - use the profiler - Instruments - CPU Sampler - to see what timing information that function shows and compare it with others as something else could be slowing things down.

Once you have confirmed your assumptions and you need more details you could add very fine grained timing using "mach_absolute_time". Report time differences at the end with NSlog. Don't do too much logging as that can hurt performance as well.

山人契 2024-10-03 23:01:26

那么,您是否只是怀疑上面的代码是缓慢的?即,您是否添加了 NSLog 调用来查看 didFinishLaunchingWithOptions 何时输入以及何时返回?只需在上面几行之间添加一些 NSLog 就会很快告诉你它卡在哪里,你不觉得吗?

另外,请注意 init... 函数首先被调用,并且在其他文件中也被调用。在那里添加 NSLogs 以查看它们是否在 didFinishLaunchingWithOptions 之前被调用,以发现它们中是否有任何浪费时间。

So, do you just suspect that the above code is what's slow? I.e, have you added NSLog calls to see when didFinishLaunchingWithOptions gets entered, and when it returns? Just adding a few NSLog between a few of above lines will quickly show you where it gets stuck, don't you think?

Also, be aware that the init... functions get called first, and +initialize in other files, too. Add NSLogs there to see if they get called before didFinishLaunchingWithOptions to spot if any of them wastes the time.

蛮可爱 2024-10-03 23:01:26

如果它正在执行任何需要一段时间的处理,则会生成一个新线程并在后台线程上执行此工作。

If it is doing any sort of processing that takes a while spawn a new thread and do this work on a background thread.

明月松间行 2024-10-03 23:01:26

尝试不使用整个 ManagedObjectContext 的应用程序,看看它是否启动得更快。如果是这样,则仅在您需要的视图打开时才设置 ManagedObjectContext。如果开始时间保持不变,那么它应该与您的 nib 文件有关。

Try the App without the whole managedObjectContext and see if it starts faster. If so then set the managedObjectContext only when the view opens in which you need it. If the starting time stays the same then it should be something with you nib file.

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