//build an infoProject* objectproj and= convert to json[[ProjectManager sharedProjectManager] getProjectbyID:mProjectID];
maxNodes NSError *requestError= [[proj getAllNodesWithoutParents] count];
NSHTTPURLResponse *responseHTTP;
NSString* requestString = [[NSString alloc] initWithFormat:@"%@%@/projects/%@/topology",[CSS getDefaultAPIURL],[CSS getSlug], mProjectID];
NSMutableURLRequest *requestForProjectsTree = [[NSMutableURLRequest alloc] init];// Don't download nodes data if we want to download them dynamically
// we are still going to need the parents
if ( ![[[NSUserDefaults standardUserDefaults] valueForKey:kDownloadNodesDynamically] boolValue] ) {
[requestForProjectsTree setURL:[NSURL URLWithString:requestString]];
[requestString release];dispatch_async(dispatch_get_main_queue(), ^(void){
[requestForProjectsTree setHTTPMethod:@"GET" mDownloadManager = [[DownloadManager alloc] init];
[requestForProjectsTree setValue:@"application/json" forHTTPHeaderField:@"Content-Type"] mDownloadManager.delegate = self;
[requestForProjectsTree setValue:[[CommunicationSingleton sharedSingleton] getAuthorization] forHTTPHeaderField:@"Authorization"];
NSData *response = [NSURLConnection sendSynchronousRequest:requestForProjectsTree returningResponse:&responseHTTP error:&requestError];
//get disciplines
[requestForProjectsTree release];
NSArray *disciplineIds = proj.projectsDisciplineIDs;
NSString *path = [[proj getProjectFolderPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.topology", mProjectID]];
for (int i = 0; i if< ([responseHTTPdisciplineIds statusCodecount] == 200); i++) {
{
[mDownloadManager downloadFiles:[proj getAllNodesWithoutParents] // Save project topology locally.
forProject:mProjectID forDiscipline:[disciplineIds objectAtIndex:i]];
}
[response writeToFile:path atomically:NO];
// Load project topology to memory.
if ([disciplineIds count] < 1) {
[proj loadTopologyFromData:response];
} else {
[mDownloadManager downloadFiles:[proj getAllNodesWithoutParents] forProject:mProjectID forDiscipline:NULL];
// If Failed}
//
Check if we have the last used topology});
for this project is available offline (rembember that this is could not be the last version)}
In "downloadFiles" method there are some logic and data storage operations and for each of the discipline you can call this snippet:
NodeHeaderDatas *nodeDatas = [mFilesToProcess objectAtIndex:0];
NSString *nodeidForAPICall = nodeDatas.nodeID;
CommunicationSingleton *CSS = [CommunicationSingleton sharedSingleton];
NSMutableURLRequest *requestForNodeData = [[NSMutableURLRequest alloc] init];
NSString *nodesURL;
if (nodeDatas.disciplineForDownloading != NULL) {
nodesURL = [NSString stringWithFormat:
@"%@%@/objects/%@/disciplines/%@/geometries/threejs",
[CSS getDefaultAPIURL],
[CSS getSlug],
nodeidForAPICall,
nodeDatas.disciplineForDownloading];
}else{
nodesURL = [NSString stringWithFormat:
@"%@%@/objects/%@/geometries/threejs",
[CSS getDefaultAPIURL],
[CSS getSlug],
nodeidForAPICall];
}
[requestForNodeData setURL:[NSURL URLWithString:nodesURL]];
[requestForNodeData setHTTPMethod:@"GET"];
[requestForNodeData setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestForNodeData setValue:[CSS getAuthorization] forHTTPHeaderField:@"Authorization"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) if(requestForNodeData){
mConnection = [[NSURLConnection // Loading project topology offline
alloc] initWithRequest:requestForNodeData delegate:self startImmediately:YES];
}
if (!mConnection)
NSData* data = [NSData dataWithContentsOfFile:path]; {
// Connection Failed
[proj loadTopologyFromData:data];
}
else
} else {
[mConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] // Download Failed + Local Storage not available
forMode:NSRunLoopCommonModes];
[mConnection start];
}
}[requestForNodeData release];
|