tôi khá mới để Objective-C & có để tự động thay đổi giá trị của @property (strong, nonatomic) NSMutableArray *allCategories
từ bên trong AFHTTPRequestOperationManager
trong success
khối.
[self.allCategories addObject:tempObject];
không thay đổi giá trị allCategories
khi lặp lại trong vòng lặp.
Biến đã được khởi tạo là self.allCategories = [[NSMutableArray alloc]init];
trong chế độ xemDidLoad.Làm cách nào để sửa đổi biến không phải cục bộ (toàn cục) từ bên trong khối?
Tôi cũng đã thử tạo biến tạm thời là
trước khi bắt đầu đối tượng __block NSMutableArray *tempCategories = [[NSMutableArray alloc]init];
AFHTTPRequestOperationManager
. tempCategories
thậm chí không giữ lại giá trị của nó.
Không thể hiểu được điều gì đang xảy ra.
Sửa
Xin lỗi vì sự bất tiện
viewDidLoad có đoạn mã sau self.allCategories = [[NSMutableArray alloc]init];
[self loadData];
Dưới đây là đoạn code
-(NSMutableArray *)loadData
{
__block NSMutableArray *tempCategories = [[NSMutableArray alloc]init];
manager = [AFHTTPRequestOperationManager manager];
[manager GET:kAPICategoryList
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// downcast id to NSMutableDictionary
NSMutableDictionary *json = (NSMutableDictionary *)responseObject;
// check if dictionary is non nil has at least 1 element
if (json != nil && [json count] >= 1) {
// NSLog(@"json:\t%@", json);
// check json is non nil & has success message
if ([json objectForKey:kAPIKeyCategoryRoot] != nil) {
NSArray *arrCategoriesRoot = [json objectForKey:kAPIKeyCategoryRoot];
// check categories has some data
if (arrCategoriesRoot.count >= 1) {
for (int i = 0; i < arrCategoriesRoot.count; i++) {
SomeModel *pCategory;
NSDictionary *dctCategorySingle = [arrCategoriesRoot objectAtIndex:i];
// check category has sub category
if ([dctCategorySingle objectForKey:kAPIKeyCategorySubCategory] != nil) {
// create category with sub category
pCategory = [[SomeModel alloc]initWithSubCategorisedCategoryID:[dctCategorySingle objectForKey:kAPIKeyCategoryID]
name:[dctCategorySingle objectForKey:kAPIKeyCategoryName]
image:kIMGCategoryDefault
subCategory:[dctCategorySingle objectForKey:kAPIKeyCategorySubCategory]];
} else{
// create just a category
pCategory = [[SomeModel alloc]initWithCategoryID:[dctCategorySingle objectForKey:kAPIKeyCategoryID]
name:[dctCategorySingle objectForKey:kAPIKeyCategoryName]
image:kIMGCategoryDefault];
} // else just
[tempCategories addObject:pCategory];
[_allCategories addObject:pCategory];
} // for
NSLog(@"categories count %lu", [self.allCategories count]);
} // if count >= 1
}
else if ([json objectForKey:kAPIRespMsgCategoryFetchErrKey] != nil) {
[Utility showAlertWithTitle:kAPIRespMsgCategoryFetchErrKey
message:[json objectForKey:kAPIRespMsgCategoryFetchErrVal]
button:kMsgButtonOkayTtl];
}
} else {
// error in login => enable login
NSLog(@"%@", kMsgNetworkEmptyJSON);
}
}
// network error
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error %@", [error localizedDescription]);
}];
NSLog(@"tempCategories count %lu", [tempCategories count]);
return tempCategories;
}
Dưới đây là hình thức đầu ra NSLog:
2015/03/19 18: 27: 17,845 MyProject [4011 : 121268] viewDidLoad
2015-03-19 18: 27: 18.133 MyProject [4011: 121268] tempCác danh mục đếm 0
2015-03-19 18: 27: 18.136 MyProject [4011: 121268] numberOfRowsInSection count 0
2015/03/19 18: 27: 18,137 MyProject [4011: 121.268] numberOfRowsInSection đếm 0
2015/03/19 18: 27: 19,019 MyProject [4011: 121.268] danh mục đếm 20
khi loadData
thúc allCategories có không dữ liệu trong đó (nil).
Bạn có thể xin vui lòng gửi mã? – jakedunc
Bạn chưa cung cấp đủ thông tin. Điều gì khiến bạn nghĩ rằng nó không thay đổi giá trị? Đăng mã thực hiện cuộc gọi đến AFHTTPRequestOperationManager của bạn, cộng với mã nơi bạn kết luận rằng lệnh gọi hàm addObject không làm bất cứ điều gì. Tôi đoán là bạn không hiểu cách khối hoàn thành không đồng bộ hoạt động như thế nào và đang mong đợi giá trị được thay đổi ngay sau khi bạn thực hiện cuộc gọi. –
Hãy cẩn thận. Bạn không thể chắc chắn những gì thread khối hoàn thành của bạn là trở lại từ, có nghĩa là bạn có thể cần phải bằng cách nào đó làm cho mảng truy cập của bạn an toàn. Đây là một trong những lý do tại sao các biến toàn cầu trong một ứng dụng async không phải là lý tưởng. – damian