Global Queue Download Pictures asynchronously
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) Event {
Dispatch_async (dispatch_get_global_queue (0, 0), ^{
NSLog (@ "%@", [Nsthread CurrentThread]);
Time-consuming operation: Put in global queue, execute asynchronously
///1. URL to determine the resource path on a network
Nsurl *url = [Nsurl urlwithstring:@ "http://fe.topit.me/e/d1/12/1170068721aa112d1el.jpg"];
//2. Through the URL can download the corresponding network resources, network resources are transmitted by the binary
NSData *data = [NSData Datawithcontentsofurl:url];
//3. Binary turn into picture
UIImage *image = [UIImage imagewithdata:data];
4. Update UI, in the main thread-"Directly add the task to the main queue, will be executed in the primary queue
Dispatch_async (Dispatch_get_main_queue (), ^{
Self.iconView.image = image;
NSLog (@ "-----%@", [Nsthread CurrentThread]);
});
});
}
1. Get the path to the plist file
Sstring *listpath=[[nsbundle Mainbundle] pathforresource:@ "textlist1.plist" oftype:nil];
2. Dictionary initialization--Initializes a dictionary based on a file
Nsdictionary *dict=[nsdictionary Dictionarywithcontentsoffile:listpath];
/**nsstring string Attribute */
[[Uitabbar appearance] Settintcolor:rgba_color (18, 191, 195, 1.0)];
Filter out the input character string.
NSString *string = [Textfield.text stringbytrimmingcharactersinset: [Nscharacterset Whitespacecharacterset]];
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////
/* Clear cache stored in cache */
-(void) didreceivememorywarning{
[Super didreceivememorywarning];
Need to do some memory cleanup work here, if not clear, will be the system flash back
Clean up the cache of pictures
[Self.imagecache removeallobjects];
Cache of cleanup operations
[Self.operationcache removeallobjects];
Cancel the task inside the download queue
[Self.opqueue cancelalloperations];
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////
The creation thread that is displayed
A new thread is created
Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];
Thread.Name = @ "Thread A"; Thread
[Thread start];
Puts a thread into a scheduled thread pool, in a ready state, waiting to be called by the Cup
Hidden threads
[Self Performselectorinbackground: @selector (longtimeoperation) Withobject:nil];
How to create an implicit method
[Self Performselectorinbackground: @selector (run2:) withobject:@ "DF"];
Class method
[Nsthread detachnewthreadselector: @selector (run2:) totarget:self withobject:@ "Hello"]
Object methods
Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];
////////////////////////////////////////////////////////////////////////////////////////////////////////////
/** get the version of the current machine **/
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions {
if ([[Uidevice currentdevice].systemversion Doublevalue] >= 8.0) {
Sign up for push notifications (local notifications/remote notifications)
Uiusernotificationsettings *settings = [Uiusernotificationsettings Settingsfortypes:uiusernotificationtypealert | Uiusernotificationtypebadge | Uiusernotificationtypesound Categories:nil];
[Application registerusernotificationsettings:settings];
Get Devicetoken
[Application registerforremotenotifications];
} else {
[Application Registerforremotenotificationtypes:uiremotenotificationtypealert | Uiremotenotificationtypebadge | Uiremotenotificationtypesound];
}
if (Launchoptions[uiapplicationlaunchoptionsremotenotificationkey]) {
Write the jump code to
}
return YES;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////
/*story Load Control */
Uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ "Common" bundle:nil];
Cmlaunchadviewcontroller *ADVC = [Storyboard instantiateviewcontrollerwithidentifier:@ "Cmlaunchadviewcontroller"];
uistoryboard* Storyboard = [Uistoryboard storyboardwithname:@ "Common" Bundle:[nsbundle Mainbundle]];
How to locate the root controller from the root control
Uinavigationcontroller *navigationcontroller = (Uinavigationcontroller *) [uiapplication sharedApplication]. Keywindow.rootviewcontroller;
Determine the last cached two-level AD switch value first
Global concurrent queues
dispatch_queue_t globalqueue = dispatch_get_global_queue (dispatch_queue_priority_default, 0);
Primary queue: belongs to serial queue
dispatch_queue_t mainqueue = Dispatch_get_main_queue ();
Timed Loop Execution Events
The Dispatch_source_set_timer method is worth mentioning is the last parameter (leeway), he tells the system we need timer trigger accuracy degree. All timers are not guaranteed to be 100% accurate, this parameter is used to tell the system you want the system to ensure the accuracy of the effort level. If you want a timer to be triggered every 5 seconds, and the more accurate the better, then you pass 0 as the parameter. In addition, if it is a recurring task, such as checking email, then you will want to check every 10 minutes, but not so accurate. So you can pass in 60 and tell the system that 60 seconds of error is acceptable. His purpose is to reduce resource consumption.
Inside the global queue
_timer = dispatch_source_create (dispatch_source_type_timer, 0, 0, globalqueue);
How often do you set the execution time
Dispatch_source_set_timer (_timer, Dispatch_time_now, 1.0 * nsec_per_sec, 0.0 * nsec_per_sec);
Execution events
Dispatch_source_set_event_handler (_timer, ^{//Timer event handler
DLog (@ "Event Handler");
if (Timeout <= 0) {
//
Dispatch_source_cancel (_timer); Cancels a timed loop timer, which causes the handle to be invoked, that is, the event is executed
Primary queue Asynchronous execution
Dispatch_async (Mainqueue, ^{
if (![ Cmadmanager shareinstance].launchadclicked) {
if (!self.skipbutton.selected) {
[Self SkipAction:self.skipButton];
}
}
});
} else {
NSString *strtime = [NSString stringwithformat:@ "%d s Skip", timeout];
Executes in the main queue
Dispatch_async (Mainqueue, ^{
[Self.skipbutton settitle:strtime Forstate:uicontrolstatenormal];
});
timeout--;
}
});
Dispatch_source_set_cancel_handler (_timer, ^{//timer canceling the processor; calling Dispatch_source_cancel when executing
DLog (@ "Cancel Handler");
});
Turn on timer
Dispatch_resume (_timer);
Resume timed cycle timer; The default state is suspended after the Dispatch Source is created and requires active recovery, otherwise the event will not be passed and will not be executed
///////////////////////////////////////////////////////////////////////////////////////
/* Array Convenience */
[Self.adlist enumerateobjectsusingblock:^ (id _nonnull obj, nsuinteger idx, BOOL * _nonnull stop) {
Cmadobject *adobj = (cmadobject *) obj;
if ([Adobj.imageurl ISEQUALTOSTRING:OBJECT.IMAGEURL])
{
sameimg = YES;
}
}];
/*userdefalut*/
[Userdefaults Setobject:ad. Adid Forkey:klaunchadid];
[Userdefaults Synchronize];
///////////////////////////////////////////////////////////////////////////////////////
Total size
unsigned long long size = 0;
Get the Cache folder path
NSString *cachespath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES). LastObject;
File Manager
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
Number of files included
NSLog (@ "%@", [Mgr Contentsofdirectoryatpath:cachespath Error:nil]);
Sub-folder is component
NSString *dirpath = [Cachespath stringbyappendingpathcomponent:@ "Mp3/mysister"];
The number of files that the subfolder contains
NSLog (@ "%@", [Filemanage Contentsofdirectoryatpath:dirpath Error:nil]);
Get the properties of a folder
File creation time, size, location
Nsdictionary *dict = [Mgr Attributesofitematpath:dirpath error:nil];
NSLog (@ "%@", dict);
size = [Mgr Attributesofitematpath:dirpath error:nil].filesize;
NSLog (@ "-------%llu", size);
Print out the path to all files
Nsarray *subpaths = [Mgr Subpathsatpath:dirpath];
NSLog (@ "%@", subpaths);
For (NSString *subpath in subpaths) {
All paths
NSString *fullsubpath = [Dirpath Stringbyappendingpathcomponent:subpath];
Accumulate file size
Size + = [Mgr Attributesofitematpath:fullsubpath error:nil].filesize;
Nsdictionary *attrs = [Mgr Attributesofitematpath:fullsubpath error:nil];
Size + = [attrs[nsfilesize] unsignedintegervalue];
}
NSLog (@ "%llu", size);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///
/* Get time */
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
Formatstr = @ "YYHHDD"
Dateformatter.dateformat = Formatstr;
nsdate* nowdate=[nsdate Date];
/*block Weak reference Correlation */
__weak cmadmanager* weakself=self;
/* Single Example */
+ (Instancetype) shareinstance{
Static Manager *sharedinstance;
Static dispatch_once_t prdictate;
Dispatch_once (&prdictate, ^{
Sharedinstance = [[Manager alloc] init];
});
return sharedinstance;
}
/* Message Notification */
[[Cmappdescription sharedinstance] addobserver:self forkeypath:@ "Showadswitch" options: nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold Context:nil];
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (onapplicationwillterminate:) Name: Uiapplicationwillterminatenotification Object:nil];
[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (setloginpage:) Name: Ksetloginpagenotification Object:nil];
How to locate the root controller and get the folder path from the root control