-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathMessageData.m
135 lines (100 loc) · 4.27 KB
/
MessageData.m
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// MessageData.m
// Oxwall Messenger
//
// Created by Thomas Ochman on 2013-09-17.
// Copyright (c) 2013 Underplot ltd. All rights reserved.
//
#import "MessageData.h"
#import "MessagesViewController.h"
#import "JSONModelLib.h"
#import "MessageFeed.h"
@interface MessageData () {
MessageFeed* _feed;
MessagesViewController* _messviev;
}
@end
@implementation MessageData
@synthesize JSONmessages;
@synthesize messages;
- (void)fetchFeed
{
//
// //One way to get the feed - this one is using the JSONModel
// NSString *conversationid = @"55";
// NSString *callURL = [NSString stringWithFormat:@"http://cloudshare.se/webservice/inbox_messages.php?conversation=%@", conversationid];
// _feed = [[MessageFeed alloc] initFromURLWithString:callURL
// completion:^(JSONModel *model, JSONModelError *err) {
//
//
//
// //json fetched
//
// NSLog(@"Loaded %@", _feed.messagesinconversation);
// //This is simply wrong
// JSONmessages = _feed.messagesinconversation;
//
// }];
// Another way of getting the feed. This works better but will still not update he self.messages in MessageViewController
//1
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
//code executed in the background
//2
NSString *conversationid = @"55";
messages = [[NSMutableArray alloc] init];
[messages addObject:[NSString stringWithFormat:@"Conversation id %@", conversationid]];
[messages addObject:@"Added before the call."];
NSString *callURL = [NSString stringWithFormat:@"http://cloudshare.se/webservice/inbox_messages.php?conversation=%@", conversationid];
NSData* messFeed = [NSData dataWithContentsOfURL:
[NSURL URLWithString:callURL]
];
//3
//4
dispatch_async(dispatch_get_main_queue(), ^{
//code executed on the main queue
//5
NSDictionary* json = nil;
if (messFeed) {
json = [NSJSONSerialization
JSONObjectWithData:messFeed
options:kNilOptions
error:nil];
JSONmessages = json;
// [self updateUIWithDictionary:json];
}
});
});
}
-(void)updateUIWithDictionary:(NSDictionary*)json {
@try {
NSLog(@"test hmmm %@", messages);
//
// NSArray* keys = [[JSONmessages valueForKey:@"messagesinconversation"] allObjects];
//
// int count = [keys count] ;
// NSLog(@"Parse count is %i", count);
// for (int i=0; i < count; i++) {
//
// NSString *message = (NSString *)JSONmessages[@"messagesinconversation"][i][@"message"];
// [messages addObject:message];
//
// }
//
// [messages addObject:@"Added after the for loop. Why?"];
}
@catch (NSException *exception) {
[[[UIAlertView alloc] initWithTitle:@"Error"
message:@"Could not parse the JSON feed."
delegate:nil
cancelButtonTitle:@"Close"
otherButtonTitles: nil] show];
NSLog(@"Exception: %@", exception);
}
@finally {
// NSString* key =@"messagecreated";
// [messages addObjectsFromArray:[[JSONmessages objectForKey:@"messagesinconversation"]valueForKey:[key stringByReplacingOccurrencesOfString:@"\n" withString:@""]]];
[messages addObject:@"Added after the call."];
NSLog(@"Final: %@", messages);
}
}
@end