Re: How to cancel a loading document in NSDocument's readFromURL:ofType:error method ?
Re: How to cancel a loading document in NSDocument's readFromURL:ofType:error method ?
- Subject: Re: How to cancel a loading document in NSDocument's readFromURL:ofType:error method ?
- From: Kyle Sluder <email@hidden>
- Date: Thu, 09 Feb 2012 10:32:55 -0800
On Thu, Feb 9, 2012 at 8:01 AM, Gilles Celli <email@hidden> wrote:
> Hi,
>
> I searched the mailing-list but didn't find an answer….so sorry if this was posted before:
>
> I've setup a document based application which can read large ASCII data files (>150MB).
>
> When opening the document the method readFromURL:ofType:error is used which then opens a small feedback window
> showing the file name and an animated progress bar with a "Cancel" NSButton.
If you're targeting Snow Leopard or later, you should override
+canConcurrentlyReadDocumentsOfType: to return YES. That will cause
-initWithContentsOfURL:ofType:error: (and therefore
-readFromURL:ofType:error:) to execute on a background thread.
Then you get to the canceling part. The traditional approach would be
to set a flag when the user clicks Cancel, and periodically check this
flag from within your -readFromURL:… implementation, returning an
NSUserCancelledError if you detect it has been set.
A more modern approach might be to use NSOperationQueue. Instead of a
loop, -readFromURL:… would divide its work into operations and enqueue
those on a queue. Clicking the Cancel button would enqueue an
operation that would shut down the -readFromURL:'s operation queue and
cause it to return an NSUserCancelledError.
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden