IOS multithreaded Security with mutable arrays

Source: Internet
Author: User

Fully from IOS multi-threaded security and variable dictionary learning

Basically the same, extrapolate

Directly above the sample code

I refer to the Internet, according to the current business needs to change.

In fact, a lot of people here like to use the category processing. I personally think that using categories is extremely easy to confuse with common methods, so in order to reduce the coupling, enhance the understanding and readability of code. It's good to create a class here alone. This is a good time to use this custom security array.

//Mensestracker////Created by HF on 2018/6/7.//Copyright 2018 Huofar. All rights reserved.//#Import<Foundation/Foundation.h>@InterfaceSyncmutablearray:nsobject//Read-only-(Nsmutablearray *) SafeArray;//determine if the object is contained-(BOOL) Containsobject: (ID) anobject;//number of collection elements-(Nsuinteger) count;//get Element-(ID) Objectatindex: (nsuinteger) index;//enumeration Elements-(Nsenumerator *) Objectenumerator;//Insert- (void) InsertObject: (ID) anobject atindex: (nsuinteger) index;//Insert- (void) AddObject: (ID) anobject;//removed from- (void) Removeobjectatindex: (nsuinteger) index;//removed from- (void) Removeobject: (ID) anobject;//removed from- (void) Removelastobject;//Replace- (void) Replaceobjectatindex: (nsuinteger) index Withobject: (ID) anobject;//Get Index-(Nsuinteger) Indexofobject: (ID) anobject; @end
////syncmutablearray.m//Mensestracker////Created by HF on 2018/6/7.//Copyright 2018 Huofar. All rights reserved.//#Import"SyncMutableArray.h"@InterfaceSyncmutablearray () @property (nonatomic, strong) dispatch_queue_t Syncqueue; @property (nonatomic, Strong) Nsmutablearray*array; @end @implementation Syncmutablearray#pragma Mark-Init method-(instancetype) initcommon{ Self= [SuperInit]; if(self) {//%p Output memory address in 16-binary format with additional prefix 0xnsstring* uuid = [nsstring stringwithformat:@ "com.huofar.array_%p", self]; //Note: _syncqueue is a parallel queue_syncqueue =dispatch_queue_create ([uuid utf8string], dispatch_queue_concurrent); }    returnSelf ;}-(instancetype) init{ Self=[self Initcommon]; if(self) {_array=[Nsmutablearray array]; }    returnSelf ;}//The other init methods are slightly#pragma mark-Data manipulation methods (which involve changing elements in an array, using asynchronous dispatch + fence blocks; reading data using synchronous Dispatch +parallel queue)-(Nsmutablearray *) safearray{__block nsmutablearray*SafeArray; Dispatch_sync (_syncqueue,^{SafeArray=_array;    }); returnSafeArray;}-(BOOL) Containsobject: (ID) anobject{__block BOOL isexist=NO; Dispatch_sync (_syncqueue,^{isexist=[_array Containsobject:anobject];    }); returnisexist;}-(Nsuinteger) count{__block nsuinteger count; Dispatch_sync (_syncqueue,^{Count=_array.count;    }); returncount;}-(ID) Objectatindex: (Nsuinteger) index{__block ID obj; Dispatch_sync (_syncqueue,^{        if(Index <[_array Count]) {obj=_array[index];    }    }); returnobj;}-(Nsenumerator *) objectenumerator{__block nsenumerator*enu; Dispatch_sync (_syncqueue,^{enu=[_array Objectenumerator];    }); returnenu;}- (void) InsertObject: (ID) anobject atindex: (nsuinteger) index{Dispatch_barrier_async (_syncqueue,^{        if(AnObject && Index <[_array Count])        {[_array insertobject:anobject atindex:index]; }    });}- (void) AddObject: (ID) anobject{dispatch_barrier_async (_syncqueue,^{        if(anobject) {[_array addobject:anobject]; }    });}- (void) Removeobjectatindex: (Nsuinteger) index{Dispatch_barrier_async (_syncqueue,^{                if(Index <[_array Count])        {[_array removeobjectatindex:index]; }    });}- (void) Removeobject: (ID) anobject{dispatch_barrier_async (_syncqueue,^{[_array removeobject:anobject];//judging legitimacy outside of oneself    });}- (void) removelastobject{Dispatch_barrier_async (_syncqueue,^{[_array removelastobject]; });}- (void) Replaceobjectatindex: (nsuinteger) index Withobject: (ID) anobject{dispatch_barrier_async (_syncqueue,^{        if(AnObject && Index <[_array Count])        {[_array replaceobjectatindex:index withobject:anobject]; }    });}-(Nsuinteger) Indexofobject: (ID) anobject{__block nsuinteger Index=Nsnotfound; Dispatch_sync (_syncqueue,^{         for(inti = 0; i < [_array count]; i + +) {            if([_array objectatindex:i] = =anobject) {Index=i;  Break;    }        }    }); returnindex;}- (void) dealloc{if(_syncqueue) {_syncqueue=NULL; }} @end

Reference

1. https://www.aliyun.com/jiaocheng/354967.html

2.76728902

IOS multithreaded Security with mutable arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.