Tuesday, December 17, 2013

Objective-C > Persistent Storage

  • According to Model-View-Controller (MVC) model, data is the controller [C].
  • If your app has data, and if that data is to presist between uses, your app will probably save the data to disk as a file or files.
  •  At a minimum, you'll probably save your data to disk when your app goes into the background, so that if your app is terminated in the background, you can load your data from disk when your app launches.
  • If your data is large, you might also release it when your app goes into the background, so as to use less memory in the background and reduce your chances of being terminated while suspended;
  • What is Sandbox?
  • Basic File Operations
    • To create folder MyFolder inside the Documents directory.
 <NSFileManager>
  • A file or directory is uniquely identified to NSFileManager using a pathname to the file. A pathname is an NSString object that can either be a relative or full pathname. A relative pathname is one that is relative to the current directory. The filename ch16/copy1.m is also a relative pathname, identifying the file copy1.m stored in the directory ch16, which is contained in the current directory.
  • Full pathnames, also known as absolute pathnames, begin with a leading /. Slash is actually a directory, called the root directory. On my Mac, the full pathname to my home directory is /Users/stevekochan. This pathname specifies three directories: / (the root directory), Users, and stevekochan.
  • We ask NSFileManager instance to create the folder if it doesn't exist already:
    • NSURL* myfolder = [docsulr URLByAppendingPathComponent:@"MyFolder"];
    • NSError* err = nil;
    • BOOL ok = [fm createDirectoryAtURL:myfolder
      • withIntermediateDirectories:YES attributes:nil error:nil];
    • // ... error-checking omitted

 <NSData / NSMutableData>
  •   When working with files, you frequently need to read data into a temporary storage area, often called a buffer.
  • When collecting data for subsequent output to a file, a storage area is also often used.
  • Foundation's NSData class provides an easy way to setup a buffer, read the contents of file into it, or write the contents of a buffer out to a file.


Exercises:

1:Modify the copy program developed in Program 16.6 so that it can accept more than one source file to be copied into a directory, like the standard Unix cp command. So, the command
90

$ copy copy1.m file1.m file2.m progs
        

should copy the three files copy1.m, file1.m, and file2.m into the directory progs. Be sure that when more than one source file is specified, the last argument is in fact an existing directory.
2:Write a command-line tool called myfind that takes two arguments. The first is a starting directory to begin the search, and the second is a filename to locate. So, the command line
90

         $ myfind /Users proposal.doc
         /Users/stevekochan/MyDocuments/proposals/proposal.doc
         $
        

begins searching the file system from /Users to locate the file proposal.doc. Print either a full path to the file if it's found (as shown) or an appropriate message if it's not.
3:Write your own version of the standard Unix tools basename and dirname.
4:Using NSProcessInfo, write a program to display all the information returned by each of its getter methods.
5:Given the NSPathUtilities.h function NSTemporaryDirectory and the NSProcessInfo method globallyUniqueString described in this chapter, add a category called TempFiles to NSString and in it define a method called temporaryFileName that returns a different, unique filename every time it is invoked.
6:Modify Program 16.7 so that the file is read and written kBufSize bytes at a time, where kBufSize is defined at the beginning of your program. Be sure to test the program on large files (that is, files larger than kBufSize bytes).
7:Open a file, read its contents 128 bytes at a time, and write it to the terminal. Use FileHandle's fileHandleWithStandardOutput method to obtain a handle for the terminal's output.
Sources: 
 http://my.safaribooksonline.com/book/programming/objective-c/0672325861/working-with-files/ch16lev1sec3#X2ludGVybmFsX0h0bWxWaWV3P3htbGlkPTAtNjcyLTMyNTg2LTElMkZjaDE2bGV2MXNlYzQmcXVlcnk9


http://www.techotopia.com/index.php/Working_with_Files_in_Objective-C


http://www.tutorialspoint.com/ios/ios_objective_c.htm

http://www.tutorialspoint.com/objective_c/objective_c_file_handling.htm

No comments:

Post a Comment