Skip to content

Commit

Permalink
Added some synchronous runloop omg wtf code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccgus committed Jul 6, 2010
1 parent 80f5dc1 commit bf3f4d5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions davtest/FMWebDAVRequest.h
Expand Up @@ -50,6 +50,7 @@ enum {
NSInteger _responseStatusCode;

BOOL _synchronous;
BOOL _rlSynchronous;

NSError *_error;
}
Expand All @@ -64,6 +65,7 @@ enum {
@property (retain) NSError *error;

+ (id) requestToURL:(NSURL*)url;
+ (id) requestToURL:(NSURL*)url delegate:(id)del;
+ (id) requestToURL:(NSURL*)url delegate:(id)del endSelector:(SEL)anEndSelector contextInfo:(id)context;

- (FMWebDAVRequest*) fetchDirectoryListingWithDepth:(NSUInteger)depth extraToPropfind:(NSString*)extra;
Expand All @@ -82,6 +84,7 @@ enum {
- (FMWebDAVRequest*) moveToDestinationURL:(NSURL*)dest;

- (FMWebDAVRequest*) synchronous;
- (FMWebDAVRequest*) rlsynchronous;

- (FMWebDAVRequest*) propfind;

Expand Down
40 changes: 37 additions & 3 deletions davtest/FMWebDAVRequest.m
Expand Up @@ -30,6 +30,17 @@ + (id) requestToURL:(NSURL*)url {
return request;
}

+ (id) requestToURL:(NSURL*)url delegate:(id)del {

FMWebDAVRequest *request = [url isFileURL] ? [[FMFileDAVRequest alloc] init] : [[FMWebDAVRequest alloc] init];;

[request setUrl:url];
[request setDelegate:del];

return request;
}


+ (id) requestToURL:(NSURL*)url delegate:(id)del endSelector:(SEL)anEndSelector contextInfo:(id)context {

FMWebDAVRequest *request = [url isFileURL] ? [[FMFileDAVRequest alloc] init] : [[FMWebDAVRequest alloc] init];;
Expand Down Expand Up @@ -60,6 +71,11 @@ - (FMWebDAVRequest*) synchronous {
return [self autorelease];
}

- (FMWebDAVRequest*) rlsynchronous {
_rlSynchronous = YES;
return [self retain];
}

- (void) sendRequest:(NSMutableURLRequest *)req {


Expand All @@ -77,7 +93,20 @@ - (void) sendRequest:(NSMutableURLRequest *)req {



if (_synchronous) {

if (_rlSynchronous) {

self.connection = [NSURLConnection connectionWithRequest:req delegate:self];
// we do this so we get our delegate callbacks.
// just regular [foo syncronous] won't work for that.
NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
while (_rlSynchronous && [currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {
// Empty
}

[self autorelease];
}
else if (_synchronous) {
NSURLResponse *response = 0x00;

self.responseData = (NSMutableData*)[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&_error];
Expand All @@ -93,9 +122,8 @@ - (void) sendRequest:(NSMutableURLRequest *)req {
}
}
else {
[NSURLConnection connectionWithRequest:req delegate:self];
self.connection = [NSURLConnection connectionWithRequest:req delegate:self];
}

}

- (FMWebDAVRequest*) createDirectory {
Expand Down Expand Up @@ -240,6 +268,7 @@ - (FMWebDAVRequest*) head {
}

- (FMWebDAVRequest*) propfind {
debug(@"%s:%d", __FUNCTION__, __LINE__);

if (!_endSelector) {
_endSelector = @selector(requestDidPropfind:);
Expand Down Expand Up @@ -341,6 +370,7 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
debug(@"%s:%d", __FUNCTION__, __LINE__);
if (self.delegate && [self.delegate respondsToSelector:@selector(request:didReceiveAuthenticationChallenge:)]) {
[self.delegate request:self didReceiveAuthenticationChallenge:challenge];
}
Expand Down Expand Up @@ -381,6 +411,8 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err
[self.delegate performSelector:_endSelector withObject:self];
}

_rlSynchronous = NO;

[self autorelease];
}

Expand All @@ -391,6 +423,8 @@ -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self.delegate performSelector:_endSelector withObject:self];
}

_rlSynchronous = NO;

[self autorelease];
}

Expand Down

0 comments on commit bf3f4d5

Please sign in to comment.