你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS 開發上傳管理器

IOS 開發上傳管理器

編輯:IOS開發綜合

由於項目需要整合多處的上傳功能,涉及到的主要有數據庫(FMDB),多線程()

1、新建項目,下載依賴庫

首先新建一個項目這裡命名為UploadManager,項目依賴庫采用CocoaPods來管理所以在終端進入UploadManager項目中,輸入

 

pod init
這時會看到項目中多了一個文件Podfile,然後打開它,其內容如下:

 

 

# Uncomment this line to define a global platform for your project
# platform :ios, '6.0'

target 'UploadManager' do

end

target 'UploadManagerTests' do

end

然後添加FMDB信息,如下:

 

 

# Uncomment this line to define a global platform for your project
# platform :ios, '6.0'

target 'UploadManager' do
pod'FMDB','2.5'
end

target 'UploadManagerTests' do

end

然後關閉項目,回到終端,輸入

 

 

pod install

會自動分析依賴庫,並下載下來,等待一段時間後,終端出現如下內容,就說明下載完畢

 

 

Analyzing dependencies

CocoaPods 0.37.1 is available.
To update use: `gem install cocoapods`

For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.

Downloading dependencies
Installing FMDB (2.5)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `UploadManager.xcworkspace` for this project from now on.

然後打開項目目錄,會看到項目中多了一些內容,打開其中的UploadManager.xcworkspace

2、多線程處理

項目中采用NSOperationQueue和NSOperation來實現多線程操作和管理,設計PendingOperations類來保存上傳任務隊列、完成任務隊列以及線程隊列,MerchantTask類自定義NSOperation來實際完成任務,其中添加一個協議MerchantTaskDelegate來實現更新UI界面的進度,代碼如下:

 

 

//
//  PendingOperations.h
//  UploadManager
//
//  Created by Soheil M. Azarpour on 8/11/12.
//  Copyright (c) 2012 iOS Developer. All rights reserved.
//

#import 

@interface PendingOperations : NSObject

@property (nonatomic, strong) NSMutableDictionary *uploadInProgress;
@property (nonatomic, strong) NSMutableDictionary *uploadFinishProgress;
@property (nonatomic, strong) NSOperationQueue *uploadQueue;


@end

//
//  PendingOperations.m
//  UploadManager
//
//  Created by Soheil M. Azarpour on 8/11/12.
//  Copyright (c) 2012 iOS Developer. All rights reserved.
//

#import "PendingOperations.h"

@implementation PendingOperations
@synthesize uploadInProgress = _uploadInProgress;
@synthesize uploadFinishProgress = _uploadFinishProgress;
@synthesize uploadQueue = _uploadQueue;



- (NSMutableDictionary *)uploadInProgress {
    if (!_uploadInProgress) {
        _uploadInProgress = [[NSMutableDictionary alloc] init];
    }
    return _uploadInProgress;
}

- (NSOperationQueue *)uploadQueue {
    if (!_uploadQueue) {
        _uploadQueue = [[NSOperationQueue alloc] init];
        _uploadQueue.name = @"Upload Queue";
        _uploadQueue.maxConcurrentOperationCount = 4;
    }
    return _uploadQueue;
}

@end

//
//  MerchantTask.h
//  UploadManager
//
//  Created by 張傑 on 15/5/8.
//  Copyright (c) 2015年 張傑. All rights reserved.
//

#import 
#import "UploadTable.h"

@protocol MerchantTaskDelegate;

@interface MerchantTask : NSOperation

@property (nonatomic, assign) id  delegate;
@property (nonatomic, readonly, strong) NSIndexPath *indexPathInTableView;
@property (nonatomic, readonly, strong) UploadTable *taskRecord;

- (id)initWithTaskRecord:(UploadTable *)record atIndexPath:(NSIndexPath *)indexPath delegate:(id) theDelegate;

-(NSString *) uploadText : (NSString*)textUrl withText:(NSString*)text;

-(NSString *) uploadPic : (NSString *)picUrl widthPicText:(NSString*)picText;

-(NSString *) sysPicInfo : (NSString *)sysUrl withSysText:(NSString*) sysText;

@end

@protocol MerchantTaskDelegate 

// 5: In your delegate method, pass the whole class as an object back to the caller so that the caller can access both indexPathInTableView and photoRecord. Because you need to cast the operation to NSObject and return it on the main thread, the delegate method canít have more than one argument.
- (void) merchantTaskDidFinish:(MerchantTask *)uploader;

@end
在ViewController中實現MerchantTaskDelegate這個協議,來實現在線程中更新UI
//
//  MerchantTask.m
//  UploadManager
//  Created by 張傑 on 15/5/8.
//  Copyright (c) 2015年 張傑. All rights reserved.
//

#import "MerchantTask.h"

@interface MerchantTask()
@property (nonatomic, readwrite, strong) NSIndexPath *indexPathInTableView;
@property (nonatomic, readwrite, strong) UploadTable *taskRecord;
@end


@implementation MerchantTask
@synthesize delegate = _delegate;
@synthesize indexPathInTableView = _indexPathInTableView;
@synthesize taskRecord = _taskRecord;

#pragma mark - Life Cycle
- (id)initWithTaskRecord:(UploadTable *)record atIndexPath:(NSIndexPath *)indexPath delegate:(id) theDelegate
{
    if (self = [super init]) {
        // 2: Set the properties.
        self.delegate = theDelegate;
        self.indexPathInTableView = indexPath;
        _taskRecord = record;
    }
    return self;
}

-(void) main{
    @autoreleasepool {
        _taskRecord.starting = TRUE;
        [self uploadText:@"http://www.ssd" withText:@"dadfasdfad"];
    }
    
}
//未處理:0;文字上傳中:1;文字上傳失敗:2;圖片未上傳:3;圖片上傳中:4;圖片上傳失敗:5;圖片同步中:6;圖片同步失敗:7
-(NSString *)uploadText:(NSString *)textUrl withText:(NSString *)text{
    _taskRecord.upload_progress = 1;
    _taskRecord.upload_status = 0;
    [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO];
    NSLog(@"開始上傳文字");
    usleep(1000*1000);
    [self uploadPic:@"http://www.pic" widthPicText:@"上傳圖片"];
    
    return @"";
}

-(NSString *)uploadPic:(NSString *)picUrl widthPicText:(NSString *)picText{
    _taskRecord.upload_progress = 3;
    _taskRecord.upload_status = 1;
    [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO];
    NSLog(@"開始上傳圖片");
    usleep(1000*1000);
    _taskRecord.upload_progress = -1;
    _taskRecord.upload_status = 1;
    _taskRecord.upload_show_text = [NSString stringWithFormat:@"%d %@", 10, @"%"];
    [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO];
    usleep(1000*2000);
    _taskRecord.upload_progress = -1;
    _taskRecord.upload_status = 1;
    _taskRecord.upload_show_text = [NSString stringWithFormat:@"%d %@", 30, @"%"];
    [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO];
    usleep(1000*3000);
    _taskRecord.upload_progress = -1;
    _taskRecord.upload_status = 1;
    _taskRecord.upload_show_text = [NSString stringWithFormat:@"%d %@", 70, @"%"];
    [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO];
    usleep(1000*4000);
    _taskRecord.upload_progress = -1;
    _taskRecord.upload_status = 1;
    _taskRecord.upload_show_text = [NSString stringWithFormat:@"%d %@", 100, @"%"];
    [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO];
    usleep(1000*5000);
    _taskRecord.upload_progress = -1;
    _taskRecord.upload_status = 2;
    _taskRecord.upload_show_text = [NSString stringWithFormat:@"%d %@", 100, @"%"];
    [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO];
    usleep(1000*5000);
    [self sysPicInfo:@"" withSysText:@""];
    return @"";
}

-(NSString *)sysPicInfo:(NSString *)sysUrl withSysText:(NSString *)sysText{
    _taskRecord.upload_progress = 6;
    _taskRecord.upload_status = 2;
    [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO];
    NSLog(@"開始同步圖片");
    usleep(1000*5000);
    _taskRecord.upload_progress = 8;
    _taskRecord.upload_status = 3;
    [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO];
    return @"";
}



@end
上面的數據是用來測試的。

 

3、數據庫設計

首先是表結構設計,如下:

 

//
//  UploadTable.h
//  UploadManager
//
//  Created by 張傑 on 15/5/8.
//  Copyright (c) 2015年 張傑. All rights reserved.
//

#import 

@interface UploadTable : NSObject

@property (nonatomic, strong) NSString *upload_id;//協議、工單、臨時工單的ID
@property (nonatomic, strong) NSString *upload_name;//協議、工單、臨時工單的商戶名稱
@property (nonatomic, strong) NSString *upload_type;//協議:0;工單:1;臨時工單:2
@property (nonatomic, strong) NSString *upload_text;//文字信息格式json
@property (nonatomic, strong) NSString *upload_img_name;//圖片的名字格式json
@property (nonatomic, strong) NSString *upload_img_sys;//圖片同步數據
@property int upload_progress;//UI的同步狀態  上傳進度:-1;未處理:0;文字上傳中:1;文字上傳失敗:2;圖片未上傳:3;圖片上傳中:4;圖片上傳失敗:5;圖片同步中:6;圖片同步失敗:7;上傳完成8
@property int upload_status;//線程中使用  0:未處理 1:文字已上傳  2:圖片已上傳  3:同步成功
@property (nonatomic, strong) NSString *upload_show_text;//上傳顯示文字的進度
@property (nonatomic, getter = isStarting) BOOL starting; // 任務是否正在進行

@end

//
//  UploadTable.m
//  UploadManager
//
//  Created by 張傑 on 15/5/8.
//  Copyright (c) 2015年 張傑. All rights reserved.
//

#import "UploadTable.h"

@implementation UploadTable

@synthesize upload_id = _upload_id;
@synthesize upload_name = _upload_name;
@synthesize upload_type = _upload_type;
@synthesize upload_text = _upload_text;
@synthesize upload_img_name = _upload_img_name;
@synthesize upload_img_sys = _upload_img_sys;
@synthesize upload_progress = _upload_progress;
@synthesize upload_status = _upload_status;
@synthesize upload_show_text = _upload_show_text;
@synthesize starting = _starting;

-(BOOL)isStarting{
    return _starting;
}

@end

數據庫操作:

 

 

//
//  DBHelper.h
//  UploadManager
//
//  Created by 張傑 on 15/5/8.
//  Copyright (c) 2015年 張傑. All rights reserved.
//

#import 
#import "FMDatabase.h"
#import "DBHelperOperation.h"

@interface DBHelper : NSObject 

+(DBHelper*) getInstance;

@end

//
//  DBHelper.m
//  UploadManager
//  創建數據庫,表結構
//  Created by 張傑 on 15/5/8.
//  Copyright (c) 2015年 張傑. All rights reserved.
//

#import "DBHelper.h"

@interface DBHelper(){
    
    FMDatabase *db;
    //數據庫表結構字段
    NSString *TABLE_NAME;//表名字
    NSString *UPLOAD_ID;//協議、工單、臨時工單的ID
    NSString *UPLOAD_NAME;//協議、工單、臨時工單的商戶名稱
    NSString *UPLOAD_TYPE;//協議:0;工單:1;臨時工單:2
    NSString *UPLOAD_TEXT;//文字信息格式json
    NSString *UPLOAD_IMG_NAME;//圖片的名字格式json
    NSString *UPLOAD_IMG_SYS;//圖片同步數據
    NSString *UPLOAD_PROGRESS;//UI的同步狀態  未處理:0;文字上傳中:1;文字上傳失敗:2;圖片未上傳:3;圖片上傳中:4;圖片上傳失敗:5;圖片同步中:6;圖片同步失敗:7
    NSString *UPLOAD_STATUS;//線程中使用  0:未處理  1:文字已更新 2:文字已上傳  3:圖片已上傳  4:同步成功
}


@end

@implementation DBHelper


+(DBHelper*) getInstance
{
    static DBHelper *instance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [[self alloc] init];
        [instance onCreate];
        [instance onCreateTable];
        NSLog(@"init");
    });
    return instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        TABLE_NAME = @"table_upload_status";
        UPLOAD_ID = @"upload_id";
        UPLOAD_NAME = @"upload_name";
        UPLOAD_TYPE = @"upload_type";
        UPLOAD_TEXT = @"upload_text";
        UPLOAD_IMG_NAME = @"upload_img_name";
        UPLOAD_IMG_SYS = @"upload_img_sys";
        UPLOAD_PROGRESS = @"upload_progress";
        UPLOAD_STATUS = @"upload_status";
    }
    return self;
}

-(void)onCreate{
    NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *dbPath   = [docsPath stringByAppendingPathComponent:@"umpad_upload_manager.db"];
    db  = [FMDatabase databaseWithPath:dbPath];
}

-(BOOL)onCreateTable{
    if ([db open]) {
        NSString *sqlCreateTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS  '%@' (id INTEGER PRIMARY KEY AUTOINCREMENT, '%@' TEXT, '%@' TEXT, '%@' TEXT, '%@' TEXT, '%@' TEXT, '%@' TEXT, '%@' INTEGER, '%@' INTEGER)", TABLE_NAME, UPLOAD_ID, UPLOAD_NAME, UPLOAD_TYPE, UPLOAD_TEXT, UPLOAD_IMG_NAME, UPLOAD_IMG_SYS, UPLOAD_PROGRESS, UPLOAD_STATUS];
        [db beginTransaction];
        BOOL res = [db executeUpdate: sqlCreateTable];
        if (res) {
            NSLog(@"創建表成功");
        }else{
            NSLog(@"創建表失敗");
        }
        [db commit];
        [db close];
        return res;
    }
    return FALSE;
}

-(BOOL)onUpgrade{
    return FALSE;
}

//插入一條數據
-(BOOL) insertOneData : (UploadTable*) table{
    if ([db open]) {
        NSString *insertSql= [NSString stringWithFormat:
                              @"INSERT INTO '%@' ('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@') VALUES ('%@', '%@', '%@', '%@', '%@', '%@', '%d', '%d')",
                              TABLE_NAME, UPLOAD_ID, UPLOAD_NAME, UPLOAD_TYPE, UPLOAD_TEXT, UPLOAD_IMG_NAME, UPLOAD_IMG_SYS, UPLOAD_PROGRESS, UPLOAD_STATUS, table.upload_id, table.upload_name, table.upload_type, table.upload_text, table.upload_img_name, table.upload_img_sys, table.upload_progress, table.upload_status];
        [db beginTransaction];
        BOOL res = [db executeUpdate:insertSql];
        if (res) {
            NSLog(@"insertOneData成功");
        }else{
            NSLog(@"insertOneData失敗");
        }
        [db commit];
        [db close];
        return res;
    }
    return FALSE;
}

//更新一條數據
-(BOOL) uploadOneData : (UploadTable*) table{
    if ([db open]) {
        NSString *updateSql = [NSString stringWithFormat:@"UPDATE '%@' SET '%@' = '%@', '%@' = '%@', '%@' = '%@', '%@' = '%@','%@' = '%d', '%@' = '%d', WHERE '%@' = '%@'", TABLE_NAME, UPLOAD_NAME, table.upload_name, UPLOAD_TEXT, table.upload_text, UPLOAD_IMG_NAME, table.upload_img_name, UPLOAD_IMG_SYS, table.upload_img_sys, UPLOAD_PROGRESS, table.upload_progress, UPLOAD_STATUS, table.upload_status, UPLOAD_ID, table.upload_id];
        [db beginTransaction];
        BOOL res = [db executeUpdate:updateSql];
        [db commit];
        [db close];
        return res;
    }
    return FALSE;
}

//刪除一條數據
-(BOOL) deleteOneData : (NSString*) upload_id{
    if ([db open]) {
        NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM '%@' WHERE '%@' = '%@'", TABLE_NAME, UPLOAD_ID, upload_id];
        [db beginTransaction];
        BOOL res = [db executeUpdate:deleteSql];
        [db commit];
        [db close];
        return res;
    }
    return FALSE;
}

//刪除所有數據
-(BOOL) deleteAllData{
    if ([db open]) {
        NSString *deleteAllSql = [NSString stringWithFormat:@"DELETE FROM '%@'", TABLE_NAME];
        [db beginTransaction];
        BOOL res = [db executeUpdate:deleteAllSql];
        [db commit];
        [db close];
        return res;
    }
    return FALSE;
}

//判斷當前數據是否存在
-(BOOL) isOneDataExist : (NSString*) upload_id{
    
    return FALSE;
}

//修改線程使用狀態
-(BOOL) modifyUploadStatus : (NSString*) upload_id andStatus : (int)status{
    if ([db open]) {
        NSString *modifyStatus = [NSString stringWithFormat:@"UPDATE '%@' SET '%@' = '%d' WHERE '%@' = '%@'", TABLE_NAME, UPLOAD_STATUS, status, UPLOAD_ID, upload_id];
        [db beginTransaction];
        BOOL res = [db executeUpdate:modifyStatus];
        [db commit];
        [db close];
        return res;
    }
    return FALSE;
}

//修改UI使用狀態
-(BOOL) modifyUploadProgressStatus : (NSString *)upload_id andStatus : (int)progressStatus{
    if ([db open]) {
        NSString *modifyProgressStatus = [NSString stringWithFormat:@"UPDATE '%@' SET '%@' = '%d' WHERE '%@' = '%@'", TABLE_NAME, UPLOAD_PROGRESS, progressStatus, UPLOAD_ID, upload_id];
        [db beginTransaction];
        BOOL res = [db executeUpdate:modifyProgressStatus];
        [db commit];
        [db close];
        return res;
    }
    return FALSE;
}

//修改兩個狀態
-(BOOL) modifyStatusAll : (NSString*)upload_id andStatus : (int)status andProgressStatus : (int)progressStatus{
    if ([db open]) {
        NSString *modifyStatusAll = [NSString stringWithFormat:@"UPDATE '%@' SET '%@' = '%d', '%@' = '%d' WHERE '%@' = '%@'", TABLE_NAME, UPLOAD_PROGRESS, progressStatus, UPLOAD_STATUS, status, UPLOAD_ID, upload_id];
        [db beginTransaction];
        BOOL res = [db executeUpdate:modifyStatusAll];
        [db commit];
        [db close];
        return res;
    }
    return FALSE;
}

//查詢一條數據
-(UploadTable*) queryDataByUploadId : (NSString*)upload_id{
    if ([db open]) {
        NSString *queryOneSql = [NSString stringWithFormat:@"SELECT * FROM '%@' WHERE '%@' = '%@'", TABLE_NAME, UPLOAD_ID, upload_id];
        FMResultSet *resultSet = [db executeQuery:queryOneSql];
        if ([resultSet next]) {
            UploadTable *result = [[UploadTable alloc]init];
            result.upload_id = [resultSet stringForColumn:UPLOAD_ID];
            result.upload_name = [resultSet stringForColumn:UPLOAD_NAME];
            result.upload_text = [resultSet stringForColumn:UPLOAD_TEXT];
            result.upload_type = [resultSet stringForColumn:UPLOAD_TYPE];
            result.upload_img_name = [resultSet stringForColumn:UPLOAD_IMG_NAME];
            result.upload_img_sys = [resultSet stringForColumn:UPLOAD_IMG_SYS];
            result.upload_progress = [resultSet intForColumn:UPLOAD_PROGRESS];
            result.upload_status = [resultSet intForColumn:UPLOAD_STATUS];
            return result;
        }
        [db close];
    }
    return nil;
}

//查詢所有的數據
-(NSMutableArray*) queryDataAll{
    NSMutableArray *restArray = [[NSMutableArray alloc] init];
    if ([db open]) {
        NSString *queryAllSql = [NSString stringWithFormat:@"SELECT * FROM '%@'", TABLE_NAME];
        FMResultSet *resultSet = [db executeQuery:queryAllSql];
        while ([resultSet next]) {
            UploadTable *result = [[UploadTable alloc]init];
            result.upload_id = [resultSet stringForColumn:UPLOAD_ID];
            result.upload_name = [resultSet stringForColumn:UPLOAD_NAME];
            result.upload_text = [resultSet stringForColumn:UPLOAD_TEXT];
            result.upload_type = [resultSet stringForColumn:UPLOAD_TYPE];
            result.upload_img_name = [resultSet stringForColumn:UPLOAD_IMG_NAME];
            result.upload_img_sys = [resultSet stringForColumn:UPLOAD_IMG_SYS];
            result.upload_progress = [resultSet intForColumn:UPLOAD_PROGRESS];
            result.upload_status = [resultSet intForColumn:UPLOAD_STATUS];
            [restArray addObject:(result)];
        }
        [db close];
    }
    return restArray;
}



@end

在DBHelper中實現了增刪改查功能,裡面的方法是在協議DBHelperOperation中:

 

 

//
//  DBHelperOperation.h
//  UploadManager
//
//  Created by 張傑 on 15/5/8.
//  Copyright (c) 2015年 張傑. All rights reserved.
//

#import "UploadTable.h"

@protocol DBHelperOperation 

//創建數據
-(void)onCreate;
//創建表
-(BOOL)onCreateTable;
//更新數據庫
-(BOOL)onUpgrade;
//插入一條數據
-(BOOL) insertOneData : (UploadTable*) table;
//更新一條數據
-(BOOL) uploadOneData : (UploadTable*) table;
//刪除一條數據
-(BOOL) deleteOneData : (NSString*) table;
//刪除所有數據
-(BOOL) deleteAllData;
//判斷當前數據是否存在
-(BOOL) isOneDataExist : (NSString*) upload_id;
//修改線程使用狀態
-(BOOL) modifyUploadStatus : (NSString*) upload_id andStatus : (int)status;
//修改UI使用狀態
-(BOOL) modifyUploadProgressStatus : (NSString *)upload_id andStatus : (int)progressStatus;
//修改兩個狀態
-(BOOL) modifyStatusAll : (NSString*)upload_id andStatus : (int)status andProgressStatus : (int)progressStatus;
//查詢一條數據
-(UploadTable*) queryDataByUploadId : (NSString*)upload_id;
//查詢所有的數據
-(NSMutableArray*) queryDataAll;

@end

4、UI界面操作

 

 

//
//  ViewController.h
//  UploadManager
//
//  Created by 張傑 on 15/5/14.
//  Copyright (c) 2015年 張傑. All rights reserved.
//

#import 
#import "DBHelper.h"
#import "PendingOperations.h"
#import "MerchantTask.h"
#import "UploadTable.h"
#import "UploadTableCell.h"

@interface ViewController : UITableViewController 

@property (nonatomic, strong) NSMutableArray *tableData; // main data source of controller
@property (nonatomic, strong) PendingOperations *pendingOperations;

@end

//
//  ViewController.m
//  UploadManager
//
//  Created by 張傑 on 15/5/14.
//  Copyright (c) 2015年 張傑. All rights reserved.
//

#import "ViewController.h"

@interface ViewController(){
    DBHelper *dbHelper;
}

@end

@implementation ViewController
@synthesize tableData = _tableData;
@synthesize pendingOperations = _pendingOperations;

- (PendingOperations *)pendingOperations {
    if (!_pendingOperations) {
        _pendingOperations = [[PendingOperations alloc] init];
    }
    return _pendingOperations;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    dbHelper = [DBHelper getInstance];
//    [dbHelper deleteAllData];
//    UploadTable *table1 = [[UploadTable alloc] init];
//    table1.upload_id = @"lskjfsfjsfa";
//    table1.upload_name = @"上海科技有限技術責任公司";
//    table1.upload_type = @"協議";
//    table1.upload_progress = 70;
//    [dbHelper insertOneData:table1];
//    UploadTable *table2 = [[UploadTable alloc] init];
//    table2.upload_id = @"33lk333";
//    table2.upload_name = @"上海榕湖全資子公司回執信息辦事處";
//    table2.upload_type = @"工單";
//    table2.upload_progress = 48;
//    [dbHelper insertOneData:table2];
    _tableData = [dbHelper queryDataAll];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}


- (void)didReceiveMemoryWarning {
    [self cancelAllOperations];
    [super didReceiveMemoryWarning];
}

//返回某個節中的行數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"uploadTableCell";
    UploadTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UploadTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    UploadTable *uploadTable = [_tableData objectAtIndex:indexPath.row];
    //    未處理:0;文字上傳中:1;文字上傳失敗:2;圖片未上傳:3;圖片上傳中:4;圖片上傳失敗:5;圖片同步中:6;圖片同步失敗:7;圖片上傳進度:8
    switch (uploadTable.upload_progress) {
        case 0:
            cell.uploadProgress.text = @"未處理";
            break;
        case 1:
            cell.uploadProgress.text = @"文字上傳中";
            break;
        case 2:
            cell.uploadProgress.text = @"文字上傳失敗";
            break;
        case 3:
            cell.uploadProgress.text = @"圖片未上傳";
            break;
        case 4:
            cell.uploadProgress.text = @"圖片上傳中";
            break;
        case 5:
            cell.uploadProgress.text = @"圖片上傳失敗";
            break;
        case 6:
            cell.uploadProgress.text = @"圖片同步中";
            break;
        case 7:
            cell.uploadProgress.text = @"圖片同步失敗";
            break;
        case 8:
            cell.uploadProgress.text = @"上傳完成";
            
            break;
        case -1:
            cell.uploadProgress.text = uploadTable.upload_show_text;
            break;
        default:
            break;
    }
    
    cell.dataName.text = uploadTable.upload_name;
    cell.dataType.text = uploadTable.upload_type;
    cell.upload_id = uploadTable.upload_id;
    [self startOperationsForTaskRecord:uploadTable atIndexPath:indexPath];
    return cell;
}

#pragma mark -
#pragma mark - Operations

- (void)startOperationsForTaskRecord:(UploadTable *)record atIndexPath:(NSIndexPath *)indexPath {
    
    if (!record.isStarting) {
        [self startTaskingForRecord:record atIndexPath:indexPath];
    }
}

- (void)startTaskingForRecord:(UploadTable *)record atIndexPath:(NSIndexPath *)indexPath {
    
    if (![self.pendingOperations.uploadInProgress.allKeys containsObject:indexPath]) {
        MerchantTask *merchantTask = [[MerchantTask alloc] initWithTaskRecord:record atIndexPath:indexPath delegate:self];
        [self.pendingOperations.uploadInProgress setObject:merchantTask forKey:indexPath];
        [self.pendingOperations.uploadQueue addOperation:merchantTask];
    }
}

#pragma mark -
#pragma mark - Cancelling, suspending, resuming queues / operations


- (void)suspendAllOperations {
    [self.pendingOperations.uploadQueue setSuspended:YES];
}


- (void)resumeAllOperations {
    [self.pendingOperations.uploadQueue setSuspended:NO];
}


- (void)cancelAllOperations {
    [self.pendingOperations.uploadQueue cancelAllOperations];
}

- (IBAction)upLoadBtn:(id)sender {
    
    
}

- (void) merchantTaskDidFinish:(MerchantTask *)uploader{
    // 1: Check for the indexPath of the operation, whether it is a download, or filtration.
    NSIndexPath *indexPath = uploader.indexPathInTableView;
    NSLog(@"upload_id: %@ ; upload_progress: %d",uploader.taskRecord.upload_name, uploader.taskRecord.upload_progress);
    // 2: Get hold of the PhotoRecord instance.
    UploadTable *theRecord = uploader.taskRecord;
    
    // 3: Replace the updated PhotoRecord in the main data source (Photos array).
    [_tableData replaceObjectAtIndex:indexPath.row withObject:theRecord];
    
    // 4: Update UI.
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    if(uploader.taskRecord.upload_status == 3){//上傳成功
        [self.pendingOperations.uploadInProgress removeObjectForKey:indexPath];
        [self.pendingOperations.uploadFinishProgress setObject:uploader forKey:indexPath];
    }
}



@end

5、實現效果:

 

\
 

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved