-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBackendFunctionRetrievemodel.j
84 lines (68 loc) · 2.51 KB
/
BackendFunctionRetrievemodel.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Created by Martin Carlberg on January 3, 2016.
* Copyright 2016, Martin Carlberg All rights reserved.
*/
@import <Foundation/CPString.j>
@import <Foundation/CPDictionary.j>
@import "BackendFunction.j"
@import <LightObject/CPManagedObjectModel+XCDataModel.j>
@global BackendModelPath
@global BackendOptions;
@global ValidatedDatabaseWithCompletionHandler
@global BackendDatabaseAdaptor
@implementation BackendFunctionRetrievemodel : CPObject<BackendFunction> {
CPString receivedData;
Function completionBlock;
CPHTTPURLResponse receivedResponse;
}
- (id)initWithSubpath:(CPString)subpath parameters:(CPDictionary)parameters {
self = [super init];
if (self) {
}
return self;
}
- (id)initWithSubpath:(CPString)subpath data:(CPData)data {
[CPException raise:BackendUnknownHTTPMethodException format:@"Unknown method PUT"];
return nil;
}
- (void)responseWithCompletionHandler:(Function/*(CPObject<HTTPResponse>)*/)aCompletionBlock {
var path = [CPManagedObjectModel modelFilePathFromModelPath:BackendModelPath];
var request = [CPURLRequest requestWithURL:path];
if (BackendOptions.verbose) console.log("Receive model from real path: " + path);
receivedData = nil;
completionBlock = aCompletionBlock;
[CPURLConnection connectionWithRequest:request delegate:self];
}
- (CPError)parameterError {
return nil;
}
- (void)connection:(CPURLConnection)connection didReceiveResponse:(CPHTTPURLResponse)response {
receivedResponse = response;
}
- (void)connection:(CPURLConnection)connection didReceiveData:(CPString)data {
if (receivedData) {
receivedData = [receivedData stringByAppendingString:data];
} else {
receivedData = data;
}
}
- (void)connectionDidFinishLoading:(CPURLConnection)connection {
var modelData = [CPData dataWithRawString:receivedData];
receivedData = nil;
if (!completionBlock) return;
if (modelData == nil) {
return completionBlock([[NotFoundHTTPResponse alloc] init]);
} else {
[BackendDatabaseAdaptor setModel:[CPManagedObjectModel objectModelFromXMLData:modelData]];
ValidatedDatabaseWithCompletionHandler(function() {
completionBlock([[HTTPDataResponse alloc] initWithData:modelData]);
});
}
}
- (void)connection:(CPURLConnection)connection didFailWithError:(id)error {
CPLog.error(@"[" + [self className] + @" " + _cmd + @"] Error: " + error);
if (completionBlock)
completionBlock(nil);
receivedData = nil;
}
@end