001.
002.
003.
004.
005.
006.
007.
008.
009.
#import "MyMemberService.h"
010.
#import <WindowsAzureMobileServices/WindowsAzureMobileServices.h>
011.
012.
013.
@interface
MyMemberService() <MSFilter>
014.
015.
@property
(
nonatomic
, strong) MSTable *table;
016.
@property
(
nonatomic
)
NSInteger
busyCount;
017.
018.
@end
019.
020.
@implementation
MyMemberService
021.
022.
@synthesize
items;
023.
024.
025.
+ (MyMemberService *)defaultService
026.
{
027.
028.
static
MyMemberService* service;
029.
static
dispatch_once_t onceToken;
030.
dispatch_once(&onceToken, ^{
031.
service = [[MyMemberService alloc] init];
032.
});
033.
034.
return
service;
035.
}
036.
037.
-(MyMemberService *)init
038.
{
039.
self
= [
super
init];
040.
041.
if
(
self
)
042.
{
043.
045.
applicationKey:@
"uJPAhkAkcTBuTyCNxaOSnDKFzkoYqB49"
];
046.
047.
048.
self
.client = [client clientWithFilter:
self
];
049.
050.
051.
self
.table = [_client tableWithName:@
"MyMember"
];
052.
053.
self
.items = [[
NSMutableArray
alloc] init];
054.
self
.busyCount = 0;
055.
}
056.
057.
return
self
;
058.
}
059.
060.
061.
062.
-(
void
)addItem:(
NSDictionary
*)item completion:(QSCompletionBlock)completion
063.
{
064.
065.
[
self
.table insert:item completion:^(
NSDictionary
*result,
NSError
*error)
066.
{
067.
[
self
logErrorIfNotNil:error];
068.
069.
NSUInteger
index = [items count];
070.
[(
NSMutableArray
*)items insertObject:result atIndex:index];
071.
072.
073.
completion();
074.
}];
075.
}
076.
077.
- (
void
)busy:(
BOOL
)busy
078.
{
079.
080.
if
(busy)
081.
{
082.
if
(
self
.busyCount == 0 &&
self
.busyUpdate !=
nil
)
083.
{
084.
self
.busyUpdate(
YES
);
085.
}
086.
self
.busyCount ++;
087.
}
088.
else
089.
{
090.
if
(
self
.busyCount == 1 &&
self
.busyUpdate !=
nil
)
091.
{
092.
self
.busyUpdate(FALSE);
093.
}
094.
self
.busyCount--;
095.
}
096.
}
097.
098.
- (
void
)logErrorIfNotNil:(
NSError
*) error
099.
{
100.
if
(error)
101.
{
102.
NSLog
(@
"ERROR %@"
, error);
103.
}
104.
}
105.
106.
107.
- (
void
)handleRequest:(
NSURLRequest
*)request
108.
next:(MSFilterNextBlock)next
109.
response:(MSFilterResponseBlock)response
110.
{
111.
112.
MSFilterResponseBlock wrappedResponse = ^(
NSHTTPURLResponse
*innerResponse,
NSData
*data,
NSError
*error)
113.
{
114.
[
self
busy:
NO
];
115.
response(innerResponse, data, error);
116.
};
117.
118.
119.
[
self
busy:
YES
];
120.
next(request, wrappedResponse);
121.
}
122.
123.
@end
ไม่มีความคิดเห็น:
แสดงความคิดเห็น