NSError *jsonError;
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // data is returned by my NSURLSessionDataTask's completion handler
NSMutableArray *resultsArray = [[jsonString componentsSeparatedByString:@"\n"] mutableCopy];
[resultsArray removeObjectAtIndex:[resultsArray count]-1]; // remove empty last object
NSMutableArray *jsonArray = [NSMutableArray array];
for (NSString *result in resultsArray) {
NSDictionary *jsonEvent = [NSJSONSerialization JSONObjectWithData:[result dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&jsonError];
[jsonArray addObject:jsonEvent];
}
BOOL transaction = [database inTransaction:^BOOL{
for (NSDictionary *event in jsonArray) {
CBLDocument *document = [database createDocument];
NSError *documentError;
NSMutableDictionary *mutableEvent = [event mutableCopy];
[mutableEvent setObject:kMPCBLDocumentTypeEvent forKey:kMPCBLDocumentKeyType];
[document putProperties:mutableEvent error:&documentError];
if (documentError) {
return NO;
}
}
return YES;
}];
However, the whole point of LDSON is that you don't have to wait for the entire response, so I'm trying to figure out the best way to go about parsing the data as it streams in, and probably calling inTransaction: on batches of 1000 (or so) event objects. These responses can be 100,000's of documents, or even millions in rare cases.
The NSURLSessionDataDelegate protocol has the didReceiveData: method but I'm struggling to properly parse the results and wondering if there is already some well established pattern for serializing LDSON as it is received.
Any input would be greatly appreciated!
On Apr 11, 2015, at 6:30 AM, Jared McFarland <jared...@gmail.com> wrote:However, the whole point of LDSON is that you don't have to wait for the entire response, so I'm trying to figure out the best way to go about parsing the data as it streams in