zoukankan      html  css  js  c++  java
  • Learning Core Data 1

    What is Core Data?

    If you want to build anything beyond the most simplistic apps for iOS, you’ll need a way to persist data. Luckily, Apple provides the Core Data framework, a persistence layer that efficiently handles the coordination of saving/updating/deleting data, as well as maintaining data integrity.

    With Core Data, you define the schema you want your data to conform to, and afterwards create objects from that schema to perform CRUD operations on. You don’t have to worry about what’s going on at the actual database level, as this is all abstracted away.

    It’s worth noting that Core Data is not itself a database. By default, it sits on top of SQLite3, a lightweight database baked into iOS. However, it can be configured to persist data to a document file or, in the case of the StackMob SDK, a URL service that persists the data elsewhere.

    Why Should I Learn Core Data?

    Before Core Data, developers had to work directly with SQLite3 to save their data. This provided many headaches; you had to write verbose SQL commands and handle the logic for CRUD operations. You were also responsible for insuring the data you saved matched your schema. By the end of it all, you had in essence written your own persistence layer! Core Data handles all the heavy lifting for you. As you persist your data all logic is managed under the hood with optimal performance in mind.

    Core Data has a little bit of a learning curve, which can dissuade developers who want to dive straight into building to use it. However, taking the time to gain an understanding of the Core Data fundamentals will pay large dividends in the long run, as it is a powerful tool that will enable you to build robust and data-driven apps.

    Lets Get Started

    The NSManagedObjectModel is where you define your schema for Core Data. In your model, you define theEntities, or classes of data in your schema. Within your Entities, you set their Attributes, which are the details of your data. Finally, you can link Entities together through Relationships.

    NSManagedObjectContext & NSPersistentStoreCoordinator

    The NSManagedObjectModel is only ⅓ of the Core Data picture. Let’s introduce the NSManagedObjectContext. You can think of NSManagedObjectContext as a “scratch pad” where all the changes happen. Here you create, read, update and delete objects from your database. None of the changes you make are actually persisted until you call the “save” method on your managed object context instance.

    So when a call to save is made, what happens next? Sitting between the managed object context and the database is the NSPersistentStoreCoordinator. The coordinator acts as an interface to the database where your data is being persisted. The persistent store coordinator grabs any objects that will be saved from the managed object context and verifies that names and types are consistent with the managed object model. It then sends the objects on their way to be persisted to the database.

    from:https://blog.stackmob.com/2012/11/iphone-database-tutorial-part-1-learning-core-data/

  • 相关阅读:
    当你进退两难的时候,你想做出决定,抛硬币,当你第一次抛了之后,还想再一次抛的时候,你就知道这个问题的答案了。
    习惯几乎可以绑住一切,只是不能绑住偶然。比如那只偶然尝了鲜血的老虎。
    10个理由让你爱上程序员
    自己不做出点样子,人家想拉你一把都不知你的手在哪里。
    WSL(Windows Subsystem for Linux)笔记一安装与使用
    Nginx(三)-正向代理与反向代理
    Nginx(二)-服务模式运行nginx之WINSW
    Nginx(一)-windows下的安装配置
    WebSocket(一)-RFC6455
    NodeJS笔记(六)-Express HTTP服务器启动后如何关闭
  • 原文地址:https://www.cnblogs.com/fengjian/p/3290919.html
Copyright © 2011-2022 走看看