Skip to content

Commit 6b4db4b

Browse files
author
Marcus S. Zarra
committed
ZDSStreamJSONParser and start of sample project
1 parent 01c95cd commit 6b4db4b

29 files changed

+2720
-0
lines changed

FJImporter/Classes/CGFAppDelegate.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// CGFAppDelegate.h
3+
// FJImporter
4+
//
5+
// Created by Marcus Zarra on 10/28/11.
6+
// Copyright (c) 2011 Cocoa Is My Girlfriend. All rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person
9+
// obtaining a copy of this software and associated documentation
10+
// files (the "Software"), to deal in the Software without
11+
// restriction, including without limitation the rights to use,
12+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to whom the
14+
// Software is furnished to do so, subject to the following
15+
// conditions:
16+
//
17+
// The above copyright notice and this permission notice shall be
18+
// included in all copies or substantial portions of the Software.
19+
//
20+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27+
// OTHER DEALINGS IN THE SOFTWARE.
28+
29+
@interface CGFAppDelegate : UIResponder <UIApplicationDelegate>
30+
31+
@property (nonatomic, retain) UIWindow *window;
32+
@property (nonatomic, retain) UINavigationController *navController;
33+
34+
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
35+
36+
- (void)saveContext;
37+
38+
@end

FJImporter/Classes/CGFAppDelegate.m

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
2+
//
3+
// CGFAppDelegate.m
4+
// FJImporter
5+
//
6+
// Created by Marcus Zarra on 10/28/11.
7+
// Copyright (c) 2011 Cocoa Is My Girlfriend. All rights reserved.
8+
//
9+
// Permission is hereby granted, free of charge, to any person
10+
// obtaining a copy of this software and associated documentation
11+
// files (the "Software"), to deal in the Software without
12+
// restriction, including without limitation the rights to use,
13+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
// copies of the Software, and to permit persons to whom the
15+
// Software is furnished to do so, subject to the following
16+
// conditions:
17+
//
18+
// The above copyright notice and this permission notice shall be
19+
// included in all copies or substantial portions of the Software.
20+
//
21+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28+
// OTHER DEALINGS IN THE SOFTWARE.
29+
30+
#import "CGFAppDelegate.h"
31+
32+
#import "CGFUsernameViewController.h"
33+
34+
@implementation CGFAppDelegate
35+
36+
@synthesize window;
37+
@synthesize managedObjectContext;
38+
@synthesize navController;
39+
40+
- (void)dealloc
41+
{
42+
[window release];
43+
[managedObjectContext release];
44+
45+
[super dealloc];
46+
}
47+
48+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
49+
{
50+
[self setWindow:[[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]];
51+
52+
CGFUsernameViewController *controller = [[CGFUsernameViewController alloc] initWithNibName:@"CGFUsernameView" bundle:nil];
53+
[controller setManagedObjectContext:[self managedObjectContext]];
54+
55+
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
56+
[self setNavController:nav];
57+
MCRelease(controller);
58+
MCRelease(nav);
59+
60+
[[self window] addSubview:[[self navController] view]];
61+
62+
[[self window] makeKeyAndVisible];
63+
64+
return YES;
65+
}
66+
67+
- (void)applicationWillResignActive:(UIApplication *)application
68+
{
69+
[self saveContext];
70+
}
71+
72+
- (void)applicationDidEnterBackground:(UIApplication *)application
73+
{
74+
[self saveContext];
75+
}
76+
77+
- (void)applicationWillTerminate:(UIApplication *)application
78+
{
79+
[self saveContext];
80+
}
81+
82+
- (void)saveContext
83+
{
84+
NSError *error = nil;
85+
86+
if (![self managedObjectContext]) return;
87+
if (![[self managedObjectContext] hasChanges]) return;
88+
89+
ZAssert([[self managedObjectContext] save:&error], @"Unresolved error %@, %@", error, [error userInfo]);
90+
}
91+
92+
#pragma mark - Core Data stack
93+
94+
- (NSManagedObjectContext *)managedObjectContext
95+
{
96+
if (managedObjectContext) return managedObjectContext;
97+
98+
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"FJImporter" withExtension:@"momd"];
99+
NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
100+
101+
NSURL *storeURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"FJImporter.sqlite"];
102+
103+
NSError *error = nil;
104+
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
105+
MCRelease(mom);
106+
107+
ZAssert([psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error], @"Unresolved error %@, %@", error, [error userInfo]);
108+
109+
managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
110+
[managedObjectContext setPersistentStoreCoordinator:psc];
111+
MCRelease(psc);
112+
113+
return managedObjectContext;
114+
}
115+
116+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// CGFTimelineViewController.h
3+
// FJImporter
4+
//
5+
// Created by Marcus Zarra on 11/3/11.
6+
// Copyright (c) 2011 Cocoa Is My Girlfriend. All rights reserved.
7+
//
8+
9+
@interface CGFTimelineViewController : UITableViewController
10+
11+
@property (nonatomic, retain) NSManagedObject *user;
12+
@property (nonatomic, assign) NSManagedObjectContext *managedObjectContext;
13+
14+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//
2+
// CGFTimelineViewController.m
3+
// FJImporter
4+
//
5+
// Created by Marcus Zarra on 11/3/11.
6+
// Copyright (c) 2011 Cocoa Is My Girlfriend. All rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person
9+
// obtaining a copy of this software and associated documentation
10+
// files (the "Software"), to deal in the Software without
11+
// restriction, including without limitation the rights to use,
12+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to whom the
14+
// Software is furnished to do so, subject to the following
15+
// conditions:
16+
//
17+
// The above copyright notice and this permission notice shall be
18+
// included in all copies or substantial portions of the Software.
19+
//
20+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27+
// OTHER DEALINGS IN THE SOFTWARE.
28+
29+
#import "CGFTimelineViewController.h"
30+
31+
@interface CGFTimelineViewController() <NSFetchedResultsControllerDelegate>
32+
33+
@property (nonatomic, retain) NSFetchedResultsController *fetchController;
34+
35+
@end
36+
37+
@implementation CGFTimelineViewController
38+
39+
- (void)viewDidLoad
40+
{
41+
[super viewDidLoad];
42+
43+
[self setTitle:[[self user] valueForKey:@"name"]];
44+
45+
if ([self fetchController]) return;
46+
47+
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Tweet"];
48+
[request setPredicate:[NSPredicate predicateWithFormat:@"ANY timeline == %@", [self user]]];
49+
50+
NSSortDescriptor *sort = [[[NSSortDescriptor alloc] initWithKey:@"createdAt" ascending:NO] autorelease];
51+
[request setSortDescriptors:[NSArray arrayWithObject:sort]];
52+
53+
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:[[self user] valueForKey:@"name"]];
54+
[controller setDelegate:self];
55+
56+
NSError *error = nil;
57+
ZAssert([controller performFetch:&error], @"Error fetching tweets: %@", error);
58+
59+
[self setFetchController:controller];
60+
MCRelease(controller);
61+
}
62+
63+
#pragma mark - Table view data source
64+
65+
- (void)configureCell:(id)cell atIndexPath:(NSIndexPath*)indexPath
66+
{
67+
NSManagedObject *tweet = [[self fetchController] objectAtIndexPath:indexPath];
68+
69+
[[cell textLabel] setText:[tweet valueForKey:@"text"]];
70+
[[cell detailTextLabel] setText:[tweet valueForKeyPath:@"author.name"]];
71+
}
72+
73+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
74+
{
75+
return [[[[self fetchController] sections] objectAtIndex:section] numberOfObjects];
76+
}
77+
78+
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
79+
{
80+
id cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
81+
82+
if (!cell) {
83+
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
84+
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
85+
}
86+
87+
[self configureCell:cell atIndexPath:indexPath];
88+
89+
return cell;
90+
}
91+
92+
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
93+
{
94+
[[self tableView] beginUpdates];
95+
}
96+
97+
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
98+
{
99+
switch(type) {
100+
case NSFetchedResultsChangeInsert:
101+
[[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
102+
break;
103+
case NSFetchedResultsChangeDelete:
104+
[[self tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
105+
break;
106+
case NSFetchedResultsChangeUpdate:
107+
[self configureCell:[[self tableView] cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
108+
break;
109+
case NSFetchedResultsChangeMove:
110+
[[self tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
111+
[[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
112+
break;
113+
}
114+
}
115+
116+
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
117+
{
118+
[[self tableView] endUpdates];
119+
}
120+
121+
@synthesize user;
122+
@synthesize managedObjectContext;
123+
@synthesize fetchController;
124+
125+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// CGFURLDelegateOperation.h
3+
//
4+
// Created by Marcus Zarra on 10/28/11.
5+
// Copyright (c) 2011 Cocoa Is My Girlfriend. All rights reserved.
6+
//
7+
// Permission is hereby granted, free of charge, to any person
8+
// obtaining a copy of this software and associated documentation
9+
// files (the "Software"), to deal in the Software without
10+
// restriction, including without limitation the rights to use,
11+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the
13+
// Software is furnished to do so, subject to the following
14+
// conditions:
15+
//
16+
// The above copyright notice and this permission notice shall be
17+
// included in all copies or substantial portions of the Software.
18+
//
19+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26+
// OTHER DEALINGS IN THE SOFTWARE.
27+
28+
@class YAJLParser;
29+
30+
@interface CGFURLDelegateOperation : NSOperation
31+
32+
@property (nonatomic, retain) NSURL *requestURL;
33+
@property (nonatomic, retain) YAJLParser *parser;
34+
@property (nonatomic, retain) NSHTTPURLResponse *response;
35+
36+
@property (nonatomic, retain) NSManagedObject *user;
37+
@property (nonatomic, assign) NSManagedObjectContext *managedObjectContext;
38+
39+
- (id)initWithRequestURL:(NSURL*)aURL andContext:(NSManagedObjectContext*)aContext;
40+
41+
@end

0 commit comments

Comments
 (0)