问:存储1000 s MapKit
注释的最佳方法是什么,这些注释将显示在地图上,并且在tableView
中也应该是可浏览的?用户还应该能够将选定的注释标记为收藏夹,并将它们显示在单独的tableView
中。
背景:我正在学习iOS编程(通过Udemy、Google和Stack溢出)。我目前正在尝试制作一个MapKit
-based应用程序,它在地图上显示了许多注释。每个注释代表特定类型的位置。出于测试目的,我制作了一个虚拟GeoJSON文件(位于应用程序中,而不是服务器上),它存储每个位置,如下所示:
{
"type" : "Feature",
"properties" : {
"location" : "Norway",
"title" : "Annotation Title",
"website" : "http://www.stackoverflow.com",
},
"geometry" : {
"type" : "Point",
"coordinates" : [10, 60]
}
},
这很好;我的虚拟对象被解析了,并在地图上显示得很好。但最终,我将有1000个真正的注释,而不仅仅是我的几个虚拟注释。
这里是我的技能不足的地方:我希望用户能够将某个特定的位置标记为最喜欢的位置,当然,这个位置应该通过应用程序更新、iOS更新以及最好是在用户的多个设备之间持续存在。
用户还应该能够浏览所有批注,按国家和州排序。
什么是处理这个问题的好方法?我试着把它分解成:
tableView
,使用第一个tableView
显示所有国家,并使用一个行插入到另一个具有该国所有状态的tableView
,而另一个segue则使用第三个tableView
中的每个注释。tableView
。发布于 2021-02-23 12:59:40
在回答问题之前,我想指出,在地图上显示1000 s的MKAnnotationView
本身也是一个完整的问题,为了避免屏幕滞后,您需要考虑使用dequeueReusableAnnotationViewWithIdentifier:
方法或MKClusterAnnotation
来处理这些注释。
现在来回答您实际要求的内容,而不是考虑如何在应用程序中存储某一功能的数据。更多地考虑如何为整个应用程序及其自身存储数据。
https://www.iosapptemplates.com/blog/ios-development/data-persistence-ios-swift -此链接讨论持久数据存储的各种选项及其优缺点。正如您所说的,您目前正在学习iOS课程,,我建议您使用CoreData,因为它是一个本地的IOS特定框架,它将很好地补充您的学习,并且很可能会被Udemy所涵盖。另一个原因是CoreData很容易通过苹果的大量文档来建立和学习,所以应该是"newby“的一个良好开端。
CoreData示例
创建模型
创建一个CoreData管理器
class CoreDataManager {
static let shared = CoreDataManager()
private init() {}
private lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "PillReminder")
container.loadPersistentStores(completionHandler: { _, error in
_ = error.map { fatalError("Unresolved error \($0)") }
})
return container
}()
var mainContext: NSManagedObjectContext {
return persistentContainer.viewContext
}
func backgroundContext() -> NSManagedObjectContext {
return persistentContainer.newBackgroundContext()
}
加载一列药丸
func loadPills() -> [Pill] {
let mainContext = CoreDataManager.shared.mainContext
let fetchRequest: NSFetchRequest<Pill> = Pill.fetchRequest()
do {
let results = try mainContext.fetch(fetchRequest)
return results
}
catch {
debugPrint(error)
}
保存一个新的丸实体
func savePill(name: String) throws {
let context = CoreDataManager.shared.backgroundContext()
context.perform {
let entity = Pill.entity()
let pill = Pill(entity: entity, insertInto: context)
pill.name = name
pill.amount = 2
pill.dozePerDay = 1
pill.lastUpdate = Date()
try context.save()
}
}
https://stackoverflow.com/questions/66332081
复制相似问题