iOS Development data Store

Source: Internet
Author: User

Original address: http://blog.csdn.net/sxhong/article/details/8223881

Preference Settings Store

nsuserdefaults and the Settingbundle nsuserdefaults that it controls are used to save some settings that he will automatically write to when.
Nsubiquitouskeyvaluestore Multi-platform sync settings, limit size 64k, turn on entitlement, unique Apple ID (also consider the case when you can't connect to your icloud network)

File storage
I. Catalogue description

<application_home>/appname.app Bundle directory, including the program itself. Something inside will cause the signature to change and restart the program. itunes does not back up this directory after initial synchronization.
<application_home>/documents/stores user documentation and program data. You can make it visible through file sharing. itunes backup.
<application_home>/documents/inbox other programs require this program to open documents. Can be read and deleted, not added to the amendment. To change, you can move it first. itunes backup.
<application_home>/library/the root directory of the non-user data file store. Use one of the standard or custom folders to back up data that is not visible to the user. Do not apply this directory to store user data. itunes backup.
~library/application Support/<bundle_id> creates managed resources and data files for the user. Using this directory to store program state information, downloaded files even users create but agree to the data you manage. Save the file automatically.
~/library/caches/<bundle_id> a directory of files used to store cached files or programs that can be easily rebuilt.
<application_home>/tmp/temporary file directory, may be deleted by the system, should not be expected to always exist. Not backed up by itunes.
Other:

Offline data
can be downloaded or recreated, but users want to be able to access the data when they are offline. stored in <application_home>/documents or <application_home>/library/private Documents and marked as "Do not backup". The data in both locations is preserved when the storage space is low, and the Do not backup property blocks itunes or icloud backups. When an app no longer needs an offline data file, it should be deleted as soon as possible to avoid wasting the user's storage space. Set the do not back to property

    1. #include <sys/xattr.h>
    2. -(BOOL) Addskipbackupattributetoitematurl: (Nsurl *) URL
    3. {
    4. Const char* FilePath = [[URL path] filesystemrepresentation];
    5. Const char* attrname = "Com.apple.MobileBackup";
    6. u_int8_t attrValue = 1;
    7. int result = Setxattr (FilePath, Attrname, &attrvalue, sizeof (AttrValue), 0, 0);
    8. return result = = 0;
    9. }
Copy Code

Get directory Address
URLsForDirectory:inDomains:method returns the directory address in the form of Nsurl
Nssearchpathfordirectoriesindomains returns a directory address in the form of a string
Nshomedirectory return to the program root directory
Nstemporarydirectory Return temp file directory

Correlation variable Definition

  1. Nssearchpathdirectory
  2. enum {
  3. Nsapplicationdirectory = 1,//supported Applications (/applications)
  4. nsdemoapplicationdirectory,//unsupported applications and demonstration versions
  5. Nsdeveloperapplicationdirectory,//developer Applications (/developer/applications)
  6. Nsadminapplicationdirectory,//system and network administration applications
  7. Nslibrarydirectory,//various user-visible documentation, support, and configuration files (/library)
  8. Nsdeveloperdirectory,//developer Resources (/developer)
  9. Nsuserdirectory,//user Home Directories (/users)
  10. nsdocumentationdirectory,//
  11. nsdocumentdirectory,//
  12. Nscoreservicedirectory,//location of Core Services (system/library/coreservices)
  13. Nsautosavedinformationdirectory = 11,//location of user ' s autosaved documents Library/autosave information
  14. Nsdesktopdirectory = 12,//
  15. Nscachesdirectory = 13,//location of discardable cache files (library/caches)
  16. Nsapplicationsupportdirectory = 14,//location of application support files (library/application support)
  17. Nsdownloadsdirectory = 15,//
  18. Nsinputmethodsdirectory = 16,//
  19. Nsmoviesdirectory = 17,//
  20. Nsmusicdirectory = 18,//
  21. Nspicturesdirectory = 19,//
  22. Nsprinterdescriptiondirectory = 20,//
  23. Nssharedpublicdirectory = 21,//
  24. Nspreferencepanesdirectory = 22,//
  25. Nsitemreplacementdirectory = 99,//
  26. Nsallapplicationsdirectory = 100,//
  27. Nsalllibrariesdirectory = 101//
  28. };
  29. typedef Nsuinteger Nssearchpathdirectory;
Copy Code
    1. enum {
    2. Nsuserdomainmask = 1,//in User home directory
    3. Nslocaldomainmask = 2,//in the current machine
    4. Nsnetworkdomainmask = hosts visible in the 4,//network
    5. Nssystemdomainmask = 8,//system directory, non-modifiable (/system)
    6. Nsalldomainsmask = 0x0ffff,//All
    7. };
    8. typedef Nsuinteger NSSEARCHPATHDOMAINMASK;
Copy Code

two. Storage mode (i). Property list (plist)
Array,dictionary,data,string,nsnumber,nsdate Wait for NS objects to be written directly to the plist file to save.
(ii). Archive
implements the Nscoding protocol (Nscopy is also implemented?). Did not achieve also succeeded. ) Codec class description Nskeyedarchiver Create
-(ID) Initforwritingwithmutabledata: (Nsmutabledata *) data//archive is written to data in
Archives
+ ( NSData *) Archiveddatawithrootobject: (ID) rootobject//Archive to data in
+ (BOOL) Archiverootobject: (ID) rootobject ToFile: ( NSString *) path//Archive to file
-(void) finishencoding//After the call cannot continue to archive data, archive end must be called (public method archived)
–outputformat// Data encoding, XML, or binary
–setoutputformat:
encoded
–encodebool:forkey:
–encodebytes:length:forkey:
– Encodeconditionalobject:forkey:
–encodedouble:forkey:
–encodefloat:forkey:
–encodeint:forkey:
– Encodeint32:forkey:
–encodeint64:forkey:
–encodeobject:forkey:
Proxy delegate
–delegate
–setdelegate:

    1. –archiver:didencodeobject:
    2. –archiverdidfinish:
    3. –archiver:willencodeobject:
    4. –archiverwillfinish:
    5. –archiver:willreplaceobject:withobject:
Copy Code

Class and class name
+ Setclassname:forclass:
+ Classnameforclass:
–setclassname:forclass:
–classnameforclass:
Abnormal
extern NSString *nsinvalidarchiveoperationexception; Nskeyedunarchiver Create
–initforreadingwithdata:
Cancel Archive
+ (ID) unarchiveobjectwithdata: (NSData *) data//object to be archived from data
+ (ID) unarchiveobjectwithfile: (NSString *) path//obtained from file
Decoding data
-(BOOL) Containsvalueforkey: (NSString *) key//whether to include the given key encoding object
–decodeboolforkey:
–decodebytesforkey:returnedlength:
–decodedoubleforkey:
–decodefloatforkey:
–decodeintforkey:
–decodeint32forkey:
–decodeint64forkey:
–decodeobjectforkey:
-(void) finishdecoding//Notification delegate decoding ends and cannot continue decoding after call
Agent delegation
–delegate
–setdelegate:

    1. –unarchiver:cannotdecodeobjectofclassname:originalclasses:
    2. –unarchiver:diddecodeobject:
    3. –unarchiver:willreplaceobject:withobject:
    4. Finishing decoding
    5. –unarchiverdidfinish:
    6. –unarchiverwillfinish:
Copy Code

Class name
+ Setclass:forclassname:
+ Classforclassname:
–setclass:forclassname:
–classforclassname:
Abnormal
NSString *nsinvalidunarchiveoperationexception;
(iii). CoreData
Three attributes of entity in the Xcdatamodeld file:
Attributes: The corresponding interface variable for the Obj-c class.
The relationship between Relationships:entity, To-one,or To-many.
Fetched properties: Filtration of the above relationship? Reference 1 file storage format coredata_extern NSString * Const Nssqlitestoretype ns_available ( 10_4, 3_0);//SQLite form Storage
Coredata_extern NSString * Const Nsxmlstoretype ns_available (10_4, NA);//iOS not available
Coredata_extern NSString * Const Nsbinarystoretype ns_available (10_4, 3_0);//binary Form Storage
Coredata_extern NSString * Const Nsinmemorystoretype ns_available (10_4, 3_0);//Basic use procedure in memory (refer to SQL terminology in parentheses)
Nsmanagedobjectmodel object gets the template of the Xcdatamodeld file set in the project (that is, what tables are in your database, and what fields are in the table)
Nspersistentstorecoordinator object based on the above template generated or corresponding to a file (database files, specific data files)
Nsmanagedobjectcontext object above the operation of the file space, additions and deletions are carried out through here
Nsentitydescription object gets a table in the context above
Nsfetchrequest object to the above table of some specific operations, additions and deletions to check
-(Nsarray *) Executefetchrequest: (nsfetchrequest *) Request Error: (NSERROR * *) error; Use this method to execute the request above
If additions and deletions changed the context, remember to save. Example of an end procedure:

  1. Nsmanagedobjectcontext *managedobjectcontext = nil;
  2. Get the template
  3. Nsurl *modelurl = [[NSBundle mainbundle] urlforresource:@ "Your_xcdatamodeld_file_name" withExtension:@ "MOMD"];
  4. nsmanagedobjectmodel* Managedobjectmodel = [[Nsmanagedobjectmodel alloc] initwithcontentsofurl:modelurl];
  5. Specific documents, Document_dicrectory should be nsurl.
  6. Nsurl *storeurl = [@ "document_dicrectory" urlbyappendingpathcomponent:@ "Sqlite.sqlite"];
  7. Nserror *error = nil;
  8. Nspersistentstorecoordinator *coordinator = [[Nspersistentstorecoordinator alloc] Initwithmanagedobjectmodel: Managedobjectmodel];
  9. Using SQLite storage
  10. if (![ Coordinator Addpersistentstorewithtype:nssqlitestoretype Configuration:nil Url:storeurl Options:nil error:& ERROR]) {
  11. Error
  12. }
  13. Else
  14. {
  15. Get the operating space
  16. Managedobjectcontext = [[Nsmanagedobjectcontext alloc] init];
  17. [Managedobjectcontext Setpersistentstorecoordinator:coordinator];
  18. }
  19. Get a concrete table
  20. Nsentitydescription *entitydescription = [nsentitydescription
  21. entityforname:@ "One_entity_name"
  22. Inmanagedobjectcontext:managedobjectcontext];
  23. Action Statement
  24. Nsfetchrequest *request = [[Nsfetchrequest alloc] init];
  25. [Request Setentity:entitydescription];
  26. Conditions for manipulating statements
  27. Nspredicate *pred = [nspredicate predicatewithformat:@ "predicate_section"];
  28. [Request setpredicate:pred];
  29. Nsmanagedobject *manageobject = nil;
  30. Perform operations to get multiple specific data
  31. Nsarray *objects = [Managedobjectcontext executefetchrequest:request error:&error];
  32. if (objects = = nil) {
  33. Error
  34. }
  35. if ([objects Count] > 0)
  36. Multiple strips, take the first one.
  37. Manageobject = [Objects objectatindex:0];
  38. Else
  39. If there is no corresponding, create
  40. Manageobject = [Nsentitydescription
  41. insertnewobjectforentityforname:@ "table_name"
  42. Inmanagedobjectcontext:managedobjectcontext];
  43. Change the value of one of her fields
  44. [Manageobject setvalue:@ "VALUE" forkey:@ "one_field_name_of_the_table"];
  45. [Request release];
  46. Save, end
  47. [Managedobjectcontext save:&error];
Copy Code

(iv). Sqlite
Introduce the SQLite library, add the header file, and use the C API operation. The tedious part is that you can only store C data formats and need to be converted back and forth. Example of a procedure:

    1. Declaring a database
    2. Sqlite3 *database;
    3. Open it
    4. if (Sqlite3_open ("Database file path", &database)! = SQLITE_OK) {
    5. Sqlite3_close (database);
    6. Error
    7. }
    8. Char *errormsg;
    9. Execute no Return statement
    10. if (sqlite3_exec (database, "Delete and modify statements", NULL, NULL, &ERRORMSG)! = SQLITE_OK) {
    11. Sqlite3_close (database);
    12. Error
    13. }
    14. Sqlite3_stmt *statement;
    15. Execute Query statement
    16. if (SQLITE3_PREPARE_V2 (database, "query statement", -1, &statement, nil) = = SQLITE_OK) {
    17. Traverse results
    18. while (sqlite3_step (statement) = = Sqlite_row) {
    19. int row = Sqlite3_column_int (statement, 0);
    20. First column of data
    21. Char *rowdata = (char *) sqlite3_column_text (statement, 1);
    22. }
    23. End traversal
    24. Sqlite3_finalize (statement);
    25. }
    26. Close the database
    27. Sqlite3_close (database);

iOS Development data Store

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.