ソースを参照

Moving error alert logic from models to controllers

Mattt Thompson 13 年 前
コミット
3d6fcc35c9

+ 1 - 1
Example/AFNetworking iOS Example.xcodeproj/project.pbxproj

@@ -189,8 +189,8 @@
 			children = (
 				F8DA09E31396AC040057D0CC /* main.m */,
 				F8129C3815910830009BFE23 /* Prefix.pch */,
-				F8129C7215910C37009BFE23 /* AppDelegate.m */,
 				F8129C7315910C37009BFE23 /* AppDelegate.h */,
+				F8129C7215910C37009BFE23 /* AppDelegate.m */,
 				F8E4696C1395739D00DB05C8 /* iOS-Info.plist */,
 			);
 			name = "Supporting Files";

+ 5 - 1
Example/AppDelegate.m

@@ -71,7 +71,11 @@ didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
     
     [self.window makeKeyAndOrderFront:self];
     
-    [Tweet publicTimelineTweetsWithBlock:^(NSArray *tweets) {
+    [Tweet publicTimelineTweetsWithBlock:^(NSArray *tweets, NSError *error) {
+        if (error) {
+            [[NSAlert alertWithMessageText:NSLocalizedString(@"Error", nil) defaultButton:NSLocalizedString(@"OK", nil) alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@",[error localizedDescription]] runModal];
+        }
+        
         self.tweetsArrayController.content = tweets;
     }];
     

+ 4 - 2
Example/Classes/Controllers/PublicTimelineViewController.m

@@ -41,8 +41,10 @@
     [_activityIndicatorView startAnimating];
     self.navigationItem.rightBarButtonItem.enabled = NO;
     
-    [Tweet publicTimelineTweetsWithBlock:^(NSArray *tweets) {
-        if (tweets) {
+    [Tweet publicTimelineTweetsWithBlock:^(NSArray *tweets, NSError *error) {
+        if (error) {
+            [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"OK", nil), nil] show];
+        } else {
             _tweets = tweets;
             [self.tableView reloadData];
         }

+ 1 - 1
Example/Classes/Models/Tweet.h

@@ -33,6 +33,6 @@
 
 - (id)initWithAttributes:(NSDictionary *)attributes;
 
-+ (void)publicTimelineTweetsWithBlock:(void (^)(NSArray *tweets))block;
++ (void)publicTimelineTweetsWithBlock:(void (^)(NSArray *tweets, NSError *error))block;
 
 @end

+ 4 - 9
Example/Classes/Models/Tweet.m

@@ -52,7 +52,7 @@
 
 #pragma mark -
 
-+ (void)publicTimelineTweetsWithBlock:(void (^)(NSArray *tweets))block {
++ (void)publicTimelineTweetsWithBlock:(void (^)(NSArray *tweets, NSError *error))block {
     [[AFTwitterAPIClient sharedClient] getPath:@"statuses/public_timeline.json" parameters:[NSDictionary dictionaryWithObject:@"false" forKey:@"include_entities"] success:^(AFHTTPRequestOperation *operation, id JSON) {
         NSMutableArray *mutableTweets = [NSMutableArray arrayWithCapacity:[JSON count]];
         for (NSDictionary *attributes in JSON) {
@@ -61,16 +61,11 @@
         }
         
         if (block) {
-            block([NSArray arrayWithArray:mutableTweets]);
+            block([NSArray arrayWithArray:mutableTweets], nil);
         }
-    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {        
-#if __IPHONE_OS_VERSION_MIN_REQUIRED
-        [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"OK", nil), nil] show];
-#else
-        [[NSAlert alertWithMessageText:NSLocalizedString(@"Error", nil) defaultButton:NSLocalizedString(@"OK", nil) alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@",[error localizedDescription]] runModal];
-#endif
+    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         if (block) {
-            block(nil);
+            block([NSArray array], error);
         }
     }];
 }