I'm writing a class which retrieves and parses a file downloaded from a server.
I have a method inside the class which parses the information, and the information is downloaded when the class is initialized.
The problem is, sometimes, the method that parses the information is called before the information itself is downloaded.
FileParser *instance = [[FileParser alloc] initWithURL:#"somewhere"];
[instance parseData];
Every time the parseData method is called directly after the class is initialized, it fails because not all of the content is available.
How can I wait until all the information is downloaded before I continue executing the parseData method, without interrupting the main thread?
Any help appreciated.
Simple: You need to either perform this entire operation on a background thread/queue, or refactor your FileParser class towards a more asynchronous design. That design would be centered around a callback mechanism, such as delegation or blocks.
you should think about using delegates.
You pass your dowloading class a delegate that will handle the downloaded data.
See here
Related
I'm setting up NSFileCoordinator and NSFilePresenter in my app so I can do file IO from my AppleWatch app safely. There are some places in my code where I write to a file a couple of times in quick succession. This is a problem in and of itself and I'm working to correct it, but I'm noticing some weird behavior in the process.
I wrap my writes like this:
//In a class that implements NSFilePresenter:
NSFileCoordinator *coord = [[NSFileCoordinator alloc]initWithFilePresenter:self];
[coord coordinateWritingItemAtURL:self.presentedItemUrl options:0 error:nil byAccessor:^(NSURL *url)
{
//do my writing here using CFWriteStreamRef or NSOutputStream
}];
On the first write, the write block happens within 1 ms. But after that, there's about a 0.5 second delay between calling coordinateWritingItemAtURL and the write block being executed.
Is this expected behavior?
Some of the documentation for NSFileCoordinator and NSFilePresenter says to use prepareForReadingItemsAtURLs:writingItemsAtURLs:options:error:byAccessor: for batch operations, but it seems weird to get such a long delay when I don't batch.
Update: This happens with reading too.
Update 2: Here is an example project reproducing the problem.
Update 3: Using this API for coordination between an app and its extension is apparently a bad idea. But the question still stands.
Referring to File System Programming Guide , you can read following:
you might want to avoid incorporating changes directly from your file
presenter method. Instead, dispatch a block asynchronously to a
dispatch queue and process the changes at a later time. This lets you
process the changes at your app’s convenience without causing
unnecessary delays to the file coordinator that initiated the change.
Of course, when saving or relinquishing control of a file (such as in
the relinquishPresentedItemToReader:,
relinquishPresentedItemToWriter:, or
savePresentedItemChangesWithCompletionHandler: methods) you should
perform all necessary actions immediately and not defer them.
I think this is your case where you are defering actions.
Possible Solution:
Please read this well , to properly handle multiple successive writing operations , the relinquishPresentedItemToWriter , can do the job , same will work with reading file , relinquishPresentedItemToReader , supposing that multiple different objects are trying to read and write the same file.
P.S :
I dont know what your app does exactly , but i hope you have read this :
If you are implementing a document-based app, you do not need to
incorporate file presenter semantics into your NSDocument subclasses.
The NSDocument class already conforms to the NSFilePresenter protocol
and implements the appropriate methods. Thus, all of your documents
automatically register themselves as presenters of their corresponding
file and do things like save changes and track changes to the
document.
Is it possible to use options NSFileCoordinatorReadingImmediatelyAvailableMetadataOnly for reading and NSFileCoordinatorWritingContentIndependentMetadataOnly for writing in some cases? Looks like this iOS8 options can help you.
I have a singleton class which is being used throughout the app. I am working on another class that needs to send data to this singleton class, but can send data in bunch which will freeze the main thread.
Should I create another instance of this singleton class or should I should I create a data import utility as a separate class?
Singletons, as the name implies, are meant to have only a single instance floating around. Data freezing the main thread should be dispatched, another instance of a class won't help that.
Create another instance all you want, but don't call it singleton anymore.
Actually you should send this data in another thread and maybe use an NSLock while the data is being sent so you don't have any access errors.
Use:
[self performSelectorOnBackGround:#selector(sendDataToSingleton:) withObject:#"data to send"];
Don't create another instance of the singleton class or the rest of your application won't have access to it since it's a singleton.
Hope it helps.
By definition you should only have 1 instance of a singleton. If it's a properly constructed singleton it should not be possible to have more than 1!
If you are running into issues where your main thread is unresponsive, break the data you need to load into smaller chucks. This way, in between loading different chunks of data, the main thread can process any events it needs to and other objects can access the data in the singleton.
You could also implement a lazy data loading mechanism, where when an object wants information from the singleton, the singleton checks if your new class is waiting to give it new information and then loads it.
I'm creating a service class in Objective-C that communicates with a HTTP server. I'm familiar with NSURLConnection and the associated classes to make an asynchronous request to a server. So far, I've used self as the delegate, responding to the four delegate methods (didReceiveResponse, didReceiveData etc.) required for the call.
The service class I'm building must be able to handle several requests at once, and respond differently to each. I'm looking for a way to achieve this, without an endless switch or if-elseif in the connectionDidFinishLoading method.
I have the idea to create a delegate class that implements the four methods described above, and give that to the NSURLConnection object to talk to. However, I want to notify the parent class when the connection finishes loading and then fire another method within the parent class. And of course, the delegate doesn't know which method this is - it could be anything.
How to proceed? Is there a way to set a selector for the delegate class, and fire that selector (which is a method) when the request is finished? How do I do such a thing?
(Creating a delegate for my delegate, and then calling a superclass method could do the trick, but I'm really looking into wildcard methods, such as a selector.)
Any help is greatly appreciated!
Regards,
Reinder
I would pass a block (^{ ... })to the delegate that it should call when the connection has finished.
You have three options:
Using a block callback
Using notifications
Using a delegate, as you already suggested.
All three are perfectly valid, but with various advantages/disadvantages. Learn them all, then decide which to use in each scenario. :-)
Often more than one solution will be chosen. For example many of Apple's classes implement a delegate method and send a notification for the same event.
--UPDATE: I've decided to give AFNetworking a try. Even though RestKit has a really nice object mapping functionality, the way the networking calls were designed have made some things difficult for us.
I'm hoping for some advice on how to organize my project that's using RestKit.
I have several REST calls from a repository class and its results get passed to controllers. For example I have a getProfile method in the repository class that is getting called from our browse view controller. The view controller is set as the delegate to the get profile calls while the repository class is set to the delegate for the restkit calls.
The problem is if the browse controller makes several get profile requests, it is difficult to distinguish which result should go to which delegate function since all the restkit calls share the same delegate method objectLoaderDidFinishLoading. I then have 4 delegates that I have to match the results of the 4 asynchronous restkit requests.
Is there any way I can use blocks so that I can pass a function to execute as the asnynrhounous result comes back so that I can assign a proper delegate? The block support that I saw allowed a block to be used before the request was sent out in rest kit but I am interested in using it for when the asynchronous result is returned.
The alternative of examining the results or setting user data and sleuthing what delegate goes with what asynchronous results seems unreliable and bulky.
You can solve your issues with disambiguating between your profile requests by using the userData opaque pointer on RKObjectLoader. That will allow you to hang any object you want on the request, which can then be used to help distinguish between multiple profile requests. Also, if those profile requests are going to different resourcePaths then you can just use the wasSentToResourcePath: method on RKObjectLoader to distinguish between them.
I just stumbled upon this question while trying to figure out this problem for my own REST interface. I'm glad I did, I'll probably use RestKit now.
I digress, back to your question. As you noted it doesn't seem like the block argument in the RKObjectManager is meant to be used this way. Instead, how about writing a class that implements RKObjectLoaderDelegate, takes in a block, and calls that block on any of the delegate calls.
Maybe something like this?
#interface MyObjectLoaderDelegate : NSObject <RKObjectLoaderDelegate>
#property (nonatomic, copy) void (^callback)(RKObjectLoader *loader, NSDictionary *objectDictionary, NSError *error)
- (id)initWithCallback:(void (^)(RKObjectLoader*, NSDictionary*, NSError*)aCallaback;
#end
And on any implemented delegate method you can execute the block. Since blocks retain scoped variables you can run code against the calling delegate.
Whatcha think?
I am not sure using blocks is the right way to solve your issue.
How about having a class GetProfile that implements RKObjectLoaderDelegate.
So you call the request from within here and set itself to be the delegate.
Then you have an objectLoader per request.
So in your view controller, each time you what GetProfile, you create an instance. And then when that instance messages your controller back (via delegates?) you know which it is.
I am just grappling with this issue as well, so am keen to hear feedback.
Switching over to AFNetworking seems to be the way to go... it was a much easier implementation for what I needed.
Let's say I'm using NSXMLParser to load a level (stored as an XML document, obviously) into my iPhone game. NSXMLParser works by assigning a delegate and sending it messages at key moments. How do I ensure that my entire level is loaded before doing anything else? I know I can make my main class the delegate and implement parserDidEndDocument, but this feels very hacky. My main class shouldn't have to know anything about the way the parsing is done! On the other hand, if I make a separate class/delegate for parsing my level, my main class has no way of knowing when the parsing is finished, unless it queries the parsing class continuously or the parsing class sends it a message. Either way, the main class would be tied to the implementation of the parsing class.
Can I hide all this event-driven business from the main class and simply make the parser return the level object when it's done? (i.e., newLevel = [[GameLevel alloc] initFromXML:xmlfile], which would in turn use NSXMLParser to load the level and then somehow return when finished.) At the moment, I'm using an external DOM parser, but I'm curious how this would be done with NSXMLParser.
Sorry if this is a stupid question -- I'm a bit new to this!
EDIT: According to the NSXMLParser class reference, the parse method, which "starts the event-driven parsing operation", returns YES if successful and NO if there's an error. Does this mean that it returns only after the parsing has finished? This would solve my problem, as I had assumed that parsing continued even after the parse method returned.
NSXMLParser blocks until the parsing is done. You don't have to do anything because the level will be loaded after the parse method returns.