通过 Core-Data 恢复注释

发布于 2024-11-16 10:37:33 字数 320 浏览 2 评论 0 原文

目前,我有一个应用程序,用户单击地图并添加带有某些字幕的注释点。然而,当手机重新启动时,所有添加的积分都会消失。我有兴趣使这些注释持久化。我一直在尝试弄清楚如何使用 Core-Data 执行此操作,但在阅读本教程后: http://www.raywenderlich.com/934/core-data-tutorial-getting-started,我有点不知道从哪里开始。

任何帮助将不胜感激,谢谢。

Currently, I have an application where a user clicks on a map and adds annotation points with certain subtitles. However, when the phone is power-cycled, all their added points are gone. I'm interested in making these annotations persistent. I've been trying to figure out how to do this with Core-Data, but after reading this tutorial here: http://www.raywenderlich.com/934/core-data-tutorial-getting-started, I'm a bit lost on where to start.

Any help would be appreciated, thanks.

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

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

发布评论

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

评论(1

南笙 2024-11-23 10:37:33

如果您设置了核心数据应用程序,则只需在 xcdatamodel 文件中创建一个实体。为您想要存储的任何内容添加属性。

latitude - double
longitude - double
title - NSString

等等,直到你得到你想要的。

当您想要添加注释时,您应该为您的实体创建一个新的核心数据对象。它看起来像这样,

Location *newLocation = (Location *)[NSEntityDescription insertNewObjectForEntityForName:@"Location" inManagedObjectContext:self.managedObjectContext];
Location.latitude = ....
// and so on to store the information you want in its attributes;

如果稍后更改值,您可以在不同的点设置属性。您只需要能够访问正确的对象即可使用注释。您应该能够通过使用 NSFetchRequest 在你的 ManagedObjectContext 中。您可以使用 NSPredicate 将对象过滤到您想要的对象。然后,当您准备退出应用程序时,保存您的上下文

NSError *error = nil;
if ([managedObjectContext save:&error]) {
    // handle the error;
}

,它将存储您添加的所有对象,以便下次打开应用程序时使用。然后,您将能够根据 ManagedObjectContext 中的对象创建注释。我希望这就是您正在寻找的东西,或者至少让您知道如何完成您想做的事情。

If you have a core data application set up, you will only need to create an entity in the xcdatamodel file. Add attributes for whatever you may want to store.

latitude - double
longitude - double
title - NSString

etc. until you have what you want.

When you want to add an annotation, you should create a new core data object for your entity. It will look something like this

Location *newLocation = (Location *)[NSEntityDescription insertNewObjectForEntityForName:@"Location" inManagedObjectContext:self.managedObjectContext];
Location.latitude = ....
// and so on to store the information you want in its attributes;

You can set the attributes at different point if you change the values at a later point. You just need to be able to access the right object to go with the annotation. You should be able to do this by using NSFetchRequest in your managedObjectContext. You can use NSPredicate to filter the objects to the one you want. Then when you are ready to exit the app, save your context using

NSError *error = nil;
if ([managedObjectContext save:&error]) {
    // handle the error;
}

which will store all the objects you've added to be used next time you open the app. You will then be able to create your annotations based on the objects in your managedObjectContext. I hope this is what you were looking for, or at least gives you an idea how to approach what you'd like to do.

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