简体   繁体   English

如何在coredata中创建数据库视图

[英]How to create database view in coredata

I have quite a few fields in an entity in core data (say the entity is called Address) such as street, number, city, and state. 我在核心数据(例如,该实体称为地址)的实体中有很多字段,例如街道,数字,城市和州。 I want the user to input a text string such that the app will search the entity Address for records which matches. 我希望用户输入文本字符串,这样应用程序将在实体地址中搜索匹配的记录。 How should I do this? 我应该怎么做?

I basically want to have a generated field (search field) which will be "number, street, city, state" and try to find a record such that this search field contains the input string. 我基本上希望有一个生成的字段(搜索字段),该字段将是“数字,街道,城市,州”,并尝试查找一条记录,使该搜索字段包含输入字符串。 I want this field to automatically updates itself when any field it contains (eg, "number", "street", "city", "state") changes. 我希望此字段在包含的任何字段(例如“数字”,“街道”,“城市”,“州”)发生更改时自动自动更新。

I was looking around on Google and found the database view thing ( https://en.wikipedia.org/wiki/View_(SQL) ) which seems promising. 我在Google上四处寻找,发现了数据库视图( https://en.wikipedia.org/wiki/View_(SQL) ),这似乎很有希望。 But I cannot find any information on how to create a database view in core data. 但是我找不到有关如何在核心数据中创建数据库视图的任何信息。 Anybody can point me a direction? 有人可以指出我的方向吗? Thanks! 谢谢!

I don't understand some parts of your questions, but my initial thoughts: 我不理解您的问题的某些部分,但我最初的想法是:

I have quite a few fields in an entity in core data (say the entity is called Address) such as street, number, city, and state. 我在核心数据(例如,该实体称为地址)的实体中有很多字段,例如街道,数字,城市和州。 I want the user to input a text string such that the app will search the entity Address for records which matches. 我希望用户输入文本字符串,这样应用程序将在实体地址中搜索匹配的记录。 How should I do this? 我应该怎么做?

I basically want to have a generated field (search field) which will be "number, street, city, state" and try to find a record such that this search field contains the input string. 我基本上希望有一个生成的字段(搜索字段),该字段将是“数字,街道,城市,州”,并尝试查找一条记录,使该搜索字段包含输入字符串。 I want this field to automatically updates itself when any field it contains (eg, "number", "street", "city", "state") changes. 我希望此字段在包含的任何字段(例如“数字”,“街道”,“城市”,“州”)发生更改时自动自动更新。

Assign your controller (or another object) as a delegate to the UITextField. 将您的控制器(或另一个对象)分配为UITextField的委托。 Override the most appropriate delegate method and then perform a fetch query on the Core Data model with the entered string. 覆盖最合适的委托方法,然后使用输入的字符串对Core Data模型执行获取查询。 Update your view in the callback with the top most result. 以最高的结果更新回调中的视图。

I was looking around on Google and found the SQL view thing which seems promising. 我在Google上四处查看,发现SQL看来很有前途。 But I cannot find any information on how to create a database view in core data. 但是我找不到有关如何在核心数据中创建数据库视图的任何信息。 Anybody can point me a direction? 有人可以指出我的方向吗? Thanks! 谢谢!

A SQL View? SQL视图? Are you talking about a database browser? 您在谈论数据库浏览器吗? DB Browser Lite has always done the job for me - it allows you to browse the schema, the data and perform SQL queries. DB Browser Lite一直为我完成这项工作-它允许您浏览架构,数据并执行SQL查询。 Print out to the console the location of the model, and then use the OS File Browser to navigate to that location. 在控制台上打印出模型的位置,然后使用OS File Browser导航到该位置。

In Objective-C, that console print out would look something like: 在Objective-C中,该控制台的输出看起来像:

     - (NSURL *)applicationDocumentsDirectory {
        return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    }

Xcode doesn't have a DB viewer per say. 每个人都说Xcode没有DB查看器。 As you probably know, you can easily edit the entities and their attritibutes in the Table View when you have the .xccdatamodelId file selected. 您可能知道,选择.xccdatamodelId文件后,可以在“表视图”中轻松编辑实体及其属性。 From that file, you can inspect the entire model and its relationships in the Graph View, almost like a UML snapshot. 从该文件中,您可以像在UML快照中一样在Graph View中检查整个模型及其关系。

You could create a computed property in your Address class and use it for your search. 您可以在Address类中创建一个计算属性,并将其用于搜索。 It will update if you change any of the attributes it uses. 如果您更改它使用的任何属性,它将更新。

var fullAddress: String {
    return "\(number) \(street) \(city ?? "") \(state ?? "")"
}

(Here I assumed city and state are optional attributes) (这里我假设城市和州是可选属性)

Regarding a Db view, Core Data is an ORM and not a SQL database so there is no such thing as a view. 关于Db视图,Core Data是一个ORM而不是SQL数据库,因此没有这样的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM