将 CLLocation 位置点保存到数组中会给我带来内存/性能问题

发布于 2024-10-11 10:40:10 字数 1518 浏览 4 评论 0原文

我在使用 iPhone GPS“runkeeper”类型的应用程序时遇到了一些问题。该应用程序几乎在所有方面都工作正常,我设计它的方式是将 CLLocation 对象添加到数组中,然后将该对象数组存储在 MySQL 数据库中。然后,当我打开地图视图时,我只需从服务器中提取数组,并通过一些操作,使用数据将图钉添加到地图中。

就像我说的,这一切都运行良好,并且在模拟器和坐在我的办公桌前的设备上运行得非常好(顺便说一句,使用仪器 - 无泄漏)。然而,当我在设备上测试该应用程序并实际离开家并沿着街道走了 15 分钟时,该应用程序在 CLLocationManager 对象停止更新且数组已发布到 MySQL 时崩溃 - 几乎可以肯定(我认为)因为内存问题。

既然如此,鉴于我是一个相对菜鸟,所以要温和,我正在寻求有关开发一种更有效的方法将对象存储到数组中的建议,以便可能大量的然后存储的位置数据不会'不要融化应用程序。

这是我正在使用的代码(精选):

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {

//note 我已经取出了确定 CLocation 对象是否有效的代码,以及其他一两个小东西

    if (recording == YES) {//the start button has been pressed but the stop button hasn't.


        if (alreadyBeenRound == NO) {

            if (firstRecord == YES) {

                //create the pointsonRoute array
                if (pointsOnRouteExists == NO) {

                    pointsOnRoute = [[NSMutableArray alloc] init];
                    pointsOnRouteExists = YES;
                }

                [pointsOnRoute removeAllObjects];

                firstRecord = NO; //after setting first record, can move on

            }

            [pointsOnRoute addObject:newLocation];

                            //more stuff taken out here

            alreadyBeenRound = YES;

        } else { 

            [pointsOnRoute addObject:newLocation];


        }

//I do release the array

I'm having a bit of a problem with an iPhone GPS 'runkeeper'- type app. In almost all ways the app works fine, the way I've designed it is to add the CLLocation objects into an array, and then store that array of objects in a MySQL database. Then, when I open up a mapview, I simply pull the array from the server and, with a little manipulation, use the data to add pins to the map.

Like I say, this has all been working fine, and works beautifully well on the simulator and on the device when sitting at my desk (and, btw, using instruments - no leaks). However, when I've tested the app on the device and actually left the house and walked 15 mins down the street, the app crashes at the point the CLLocationManager object stops updating and the array is POSTed to MySQL - almost certainly (I think) because of memory problems.

That being the case, and given I'm a relative noob, so be gentle, I'm looking for advice around developing a more efficient way of storing the objects into an array so that the probably huge amount of then stored location dat doesn't melt the app.

Here's (a selection) of the code I'm using:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {

//note I have taken out the code determining whether CLocation object is valid, and one or two other little things as well

    if (recording == YES) {//the start button has been pressed but the stop button hasn't.


        if (alreadyBeenRound == NO) {

            if (firstRecord == YES) {

                //create the pointsonRoute array
                if (pointsOnRouteExists == NO) {

                    pointsOnRoute = [[NSMutableArray alloc] init];
                    pointsOnRouteExists = YES;
                }

                [pointsOnRoute removeAllObjects];

                firstRecord = NO; //after setting first record, can move on

            }

            [pointsOnRoute addObject:newLocation];

                            //more stuff taken out here

            alreadyBeenRound = YES;

        } else { 

            [pointsOnRoute addObject:newLocation];


        }

//I do release the array

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

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

发布评论

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

评论(2

哆兒滾 2024-10-18 10:40:10

只是一个疯狂的猜测。当您坐在办公桌前时,更新会在不久后停止,因为您的位置没有改变(取决于您如何设置位置管理器)。所以你的数组中没有那么多点。当你搬家时,你可能会得到很多。当您在办公桌前时,您可以检查阵列中有多少插入以及在移动时有多少插入......

Just a wild guess. When you sit at your desk the updates stop after a short while since your location is not changing (depending on how you set up the location manager). So you don't get so many points in your array. When you move you may get a lot of them. You could check when you are at your desk how many inserts in your array you get and how many while moving...

凉城已无爱 2024-10-18 10:40:10

您必须仅存储主要更新。假设您将存储第一公里每 5 米的差异(如果用户只是短途步行),如果用户跑步则每 20-50 米存储一次。如果您的数组/CoreData 开始显着增长,您还可以在后台线程上修剪一些接近的值或相同方向的值。

You have to store only major updates. Lets say you will be storing every every 5 meter difference on the first kilometre (in case the user goes just for a short walk) and than every 20-50 meters if they went for a run. You can also trim some close values or values in the same direction on a background thread shall your array/CoreData start growing significantly.

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