Asynchronous Loading

12 views
Skip to first unread message

casualdistr...@gmail.com

unread,
Apr 23, 2009, 10:10:48 PM4/23/09
to ObjectiveResource
Just wanted to let you know I tried the async loading
ConnectionManager in 1.1

It worked great....much better ui...

Thanks for your work on this project :-)

Adam Alexander

unread,
Apr 23, 2009, 10:41:44 PM4/23/09
to ObjectiveResource
Glad to hear it, please feel free to post any usability suggestions
you have as the ConnectionManager is young and can still be improved
according to how we all decide it should best integrate. Especially
if you find yourself repeating a lot of your client code in your use
of this class, maybe that is a clue that something can be brought in
to the framework to allow the client code to be even simpler. Thanks
for taking the time to post.

Adam

On Apr 23, 10:10 pm, "casualdistractionga...@gmail.com"

jcnnghm

unread,
Apr 29, 2009, 2:24:39 PM4/29/09
to ObjectiveResource
I just got done integrating the ConnectionManager into my code as
well. The interface is great, and I don't think any additional
framework integration is necessary. As I am doing quite a bit of CRUD
with TableViewControllers like the example app, I ended up subclassing
UITableViewController with a CollectionViewController, which
automatically sets up a number of things for all of my custom
controllers that subclass it, including setting up a
ConnectionManager, and asynchronous collection loading. Basically, in
my custom controllers, all I would have to do is implement a
loadCollection method which my superclass would run with the
ConnectionManager. I think the way it's setup now is as deep as the
framework abstraction should really go. It may be worth expanding the
sample application a bit, and then doing something similar to DRY it
up as a best practice.

Great framework by the way. This really makes app development much
faster.

Justin

Adam Alexander

unread,
Apr 29, 2009, 3:57:15 PM4/29/09
to ObjectiveResource
I like those ideas Justin; the types of things you're doing could be
the origin of an ObjectiveScaffold project =) That is something I
would be interested to work on as time allows.

Adam

Michael Shannon Potter

unread,
May 1, 2009, 4:11:38 PM5/1/09
to ObjectiveResource
Forgive my ignorance, but what is ConnectionManager, and where can I
access it? :-\

Jon Maddox

unread,
May 1, 2009, 4:26:19 PM5/1/09
to objectiv...@googlegroups.com
ConnetionManager is a class that encapsulates an OperationQueue. Its
in the 1.1 branch.

Its a singleton used to just pass off all your networking code to get
an async operation.


Here's a common pattern i'm using in my controllers:

- (void)viewDidLoad {
[super viewDidLoad];
[[ConnectionManager sharedInstance] runJob:@selector(loadItems)
onTarget:self];
}

- (void)loadItems{
self.teams = [league findAllTeams];
[self.tableView performSelectorOnMainThread:@selector(reloadData)
withObject:nil waitUntilDone:NO];
}


The beauty is that, what i really did was just created a generic
TableViewController with the connection manager stuff in it, and then
i subclass that for all my networked views. Then i just implement
'loadItems'. Its really nice.

Kevin Elliott

unread,
May 1, 2009, 4:28:31 PM5/1/09
to objectiv...@googlegroups.com
Are you spawning additional threads inside of reloadData? Because if
not, you're going to see some UI lag, depending on what your process
is doing and where it's loading data. All user interface actions are
performed on the main thread, so it can be dangerous if you're not
spawning off additional worker threads.

-Kevin

Jon Maddox

unread,
May 1, 2009, 4:30:29 PM5/1/09
to objectiv...@googlegroups.com
I'm not doing anything. Its just the tableView's normal reloadData
method, and i'm running that on the main thread.

Am I missing something?

jcnnghm

unread,
May 1, 2009, 4:31:04 PM5/1/09
to ObjectiveResource
That's exactly what the ConnectionManager is there for.

Justin

Kevin Elliott

unread,
May 1, 2009, 4:35:44 PM5/1/09
to objectiv...@googlegroups.com
No, that should be OK in most cases. If you override reloadData with
something that is fetching data, that's when you'd interrupt the main
thread (and thus the user interface) and see stuttery or lockup
activity in the UI. In your case, I didn't realize that reloadData
wasn't a custom method. I wasn't sure which class it was inheriting
from.

-Kevin

Adam Alexander

unread,
May 1, 2009, 4:41:04 PM5/1/09
to ObjectiveResource
I agree, in Jon's example, the expensive (network-intensive) work is
being done inside loadItems so that is the method being passed to the
ConnectionManager to run asynchronously. This method populates
self.teams with the data retrieved from ObjectiveResource, then when
complete it starts reloadData back on the main thread. I assume
reloadData simply repopulates the local UITableView from the local
self.teams data so this should not be resource intensive. Since it's
UI work I believe it is necessary to run it on the main thread in any
case. Jon's approach is the same one I've been using so I believe
(and hope!) it is the best way.

Michael Shannon Potter

unread,
Jun 17, 2009, 3:54:50 PM6/17/09
to ObjectiveResource
I must be doing something wrong. [ [ ConnectionManager
sharedInstance ] runJob:@selector( validateUser )
onTarget:ActiveUser ], for example, results in EXC_BAD_ACCESS on the
__opLock thread when [operationQueue addOperation:operation] is
called. I'm running on iPhone OS 3.0. Any ideas?

Michael Shannon Potter

unread,
Jun 17, 2009, 4:14:54 PM6/17/09
to ObjectiveResource
Never mind. It helps to have a valid target-selector combination if
you hope to run a valid method. Excuse me while I go sit in the corner
with the office DUNCE hat on...

On Jun 17, 3:54 pm, Michael Shannon Potter <mspot...@gmail.com> wrote:
> I must be doing something wrong. [ [ConnectionManager
> sharedInstance ] runJob:@selector( validateUser )
> onTarget:ActiveUser ], for example, results in EXC_BAD_ACCESS on the
> __opLock thread when [operationQueue addOperation:operation] is
> called. I'm running on iPhone OS 3.0. Any ideas?
>
> On May 1, 4:41 pm, Adam Alexander <adama...@gmail.com> wrote:
>
>
>
> > I agree, in Jon's example, the expensive (network-intensive) work is
> > being done inside loadItems so that is the method being passed to the
> >ConnectionManagerto run asynchronously.  This method populates
> > > >>> [[ConnectionManagersharedInstance] runJob:@selector(loadItems)
> > > >>> onTarget:self];
> > > >>> }
>
> > > >>> - (void)loadItems{
> > > >>> self.teams = [league findAllTeams];
> > > >>> [self.tableView performSelectorOnMainThread:@selector(reloadData)
> > > >>> withObject:nil waitUntilDone:NO];
> > > >>> }
>
> > > >>> The beauty is that, what i really did was just created a generic
> > > >>> TableViewController with the connection manager stuff in it, and  
> > > >>> then
> > > >>> i subclass that for all my networked views. Then i just implement
> > > >>> 'loadItems'. Its really nice.
>
> > > >>> On May 1, 2009, at 4:11 PM, Michael Shannon Potter wrote:
>
> > > >>>> Forgive my ignorance, but what isConnectionManager, and where  
> > > >>>> can I
> > > >>>> access it? :-\
>
> > > >>>> On Apr 29, 3:57 pm, Adam Alexander <adama...@gmail.com> wrote:
> > > >>>>> I like those ideas Justin; the types of things you're doing could
> > > >>>>> be
> > > >>>>> the origin of an ObjectiveScaffold project =)  That is something I
> > > >>>>> would be interested to work on as time allows.
>
> > > >>>>> Adam
>
> > > >>>>> On Apr 29, 2:24 pm, jcnnghm <jcnn...@gmail.com> wrote:
>
> > > >>>>>> I just got done integrating theConnectionManagerinto my code as
> > > >>>>>>> you have as theConnectionManageris young and can still be
> > > >>>>>>> improved
> > > >>>>>>> according to how we all decide it should best integrate.
> > > >>>>>>> Especially
> > > >>>>>>> if you find yourself repeating a lot of your client code in your
> > > >>>>>>> use
> > > >>>>>>> of this class, maybe that is a clue that something can be  
> > > >>>>>>> brought
> > > >>>>>>> in
> > > >>>>>>> to the framework to allow the client code to be even simpler.
> > > >>>>>>> Thanks
> > > >>>>>>> for taking the time to post.
>
> > > >>>>>>> Adam
>
> > > >>>>>>> On Apr 23, 10:10 pm, "casualdistractionga...@gmail.com"
>
> > > >>>>>>> <casualdistractionga...@gmail.com> wrote:
> > > >>>>>>>> Just wanted to let you know I tried the async loading
> > > >>>>>>>>ConnectionManagerin 1.1

Adam Alexander

unread,
Jun 17, 2009, 4:16:59 PM6/17/09
to objectiv...@googlegroups.com
I was about to ask you something along those lines, mostly because I've had entirely too many of those moments myself =)

Adam

Jon Maddox

unread,
Jun 17, 2009, 4:23:08 PM6/17/09
to objectiv...@googlegroups.com
while the topic is in the air, I haven't had much luck canceling all the operations on the ConnectionManager. When i use   [[ConnectionManager sharedInstance] cancelAllJobs]; it doesn't seem to really respect it. Request connections don't drop and the full methods just keep going.

Adam Alexander

unread,
Jun 17, 2009, 4:42:36 PM6/17/09
to objectiv...@googlegroups.com
Thanks Jon, that is good to know.  From what I understand the way it is now if you have more than one operation in the queue it should be able to drop any operations that haven't yet started executing.  For operations that are already executing, it seems we would probably need to add a little logic to have OR check for the cancellation message in order to get the behavior you're describing.

Jon Maddox

unread,
Jun 17, 2009, 4:51:14 PM6/17/09
to objectiv...@googlegroups.com
ok good then i'm not crazy. Yeah, ideally you'd be able to cancel everything thats going on so that if a user leaves a view, the operation can stop, or if a user toggles something the requests dont just pile up, we'll want to stop what we're doing and start the new one.

ASIHTTPRequest handles their connections in an operation queue and has this functionality, its crucial for handling requests and preventing crashes:

Reply all
Reply to author
Forward
0 new messages