Cocoa read file descriptor




















However, you should use them only when performing a single read or write operation on a file. Dispatch sources allow you to process files in a way that is similar to Cocoa stream objects. Like stream objects, they are used more often with sockets or data sources that send and receive data sporadically but they can still be used with files.

A dispatch source schedules its associated event handler block whenever there is data waiting to be read or space available for writing. For files, this usually results in the block being scheduled repeatedly and in quick succession until you explicitly cancel the dispatch source or it reaches the end of the file it is reading.

For more information about creating and using dispatch sources, see Concurrency Programming Guide. If you already have an open file descriptor, passing it to the channel changes the ownership of that file descriptor from your code to the channel. Creating a channel using a file path causes the channel to create the necessary file descriptor and take control of it. In this case, the channel is configured for random read-access and assigned to a custom property of the current class.

If an error occurs during creation, you can use the error code to determine what happened. If you created a channel that supports random access, you can start reading or writing at any location. If you create a stream-based channel, any offset value you specify as a starting point is ignored and data is read or written at the current location.

For example, to read the second bytes from a channel that supports random access, your read call might look similar to the following:. A write operation requires you to specify the bytes you want written to the file, the location at which to begin writing for random access channels , and a handler block with which to receive progress reports. The use of an opaque type allows GCD to use discontiguous buffers internally while still presenting the data to your app as if it were more or less contiguous.

The actual implementation details of how dispatch data structures work is not important, but understanding how to create them or get data out of them is. If you use the default destructor, the data object makes a copy of the buffer and takes care of releasing that buffer at the appropriate time.

However, if you do not want the data object to copy the buffer you provide, you must provide a custom destructor block to handle any needed cleanup when the data object itself is released. Because dispatch data objects are opaque, you use this function to iterate over the buffers in the object and process them using a block that you provide. For a dispatch data object with a single contiguous buffer, your block is called once. For a data object with multiple buffers, your block is called as many times as there are buffers.

Each time your block is called, it is passed a data buffer and some information about that buffer. Listing shows an example that opens a channel and reads a UTF8 formatted text file, creating NSString objects for the contents of the file.

This particular example reads bytes at a time, which is an arbitrary amount and may not yield the best performance. It then hands the string off to the custom addString:toFile: method, which in this case would store it for later use.

Just because they execute synchronously does not mean that they are less efficient than their asynchronous counterparts. On the contrary, it just means that the interface itself does not provide the asynchronous execution context automatically.

If you want to read and write data asynchronously using these technologies, and get all the same benefits, you must provide the asynchronous execution context yourself. Of course, the best way to do that is to execute your code using GCD dispatch queues or operation objects. The simplest way to manage the reading and writing of file data is all at once. You would not want to do this for multimedia files or for files whose size can grow to many megabytes in size.

For custom document types that use a binary or private file format, you can use the NSData or NSMutableData class to transfer your custom data to and from disk. You can create new data objects in many different ways. For example, you can use a keyed archiver object to convert a graph of objects into a linear stream of bytes enclosed in a data object. If you have a binary file format that is very structured, you can append bytes to an NSMutableData object and build your data object piece by piece.

These methods allow you to create the corresponding on-disk file in one step. To read data back from disk, use the initWithContentsOfURL:options:error: method to obtain a data object based on the contents of your file. Learn more. Ask Question. Asked 10 years, 3 months ago. Active 10 years, 3 months ago.

Viewed 3k times. Improve this question. Rob Keniger John John 3 3 gold badges 14 14 silver badges 29 29 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Assembly - File Management Advertisements. Previous Page. Next Page. Useful Video Courses. More Detail. Previous Page Print Page. Save Close.



0コメント

  • 1000 / 1000