ios


Need to share data from (A) thread to (B) Thread using +ios (objective c or swift)


I need to share data from 1 thread to another running thread. it's possible in ios(objective c or swift). If its possible then anyone please suggest me.
I have A-thread & B-Thread both are running parallel.
In A-thread have (XYZ Object).
Need to pass on B-Thread in running process.
Suggest me best solution or any blogs, tutorial, example etc....
Both A and B can refer to a class containing this variable. You can then make this variable volatile, and this means that changes to that variable are immeditately visible in both threads.
See this article for more info
Volatile variables share the visibility features of synchronized, but none of the atomicity features. This means that threads will automatically see the most up-to-date value for volatile variables. They can be used to provide thread safety, but only in a very restricted set of cases: those that do not impose constraints between multiple variables or between a variable's current value and its future values.
You have to create a model class of type NSObject. this model class will contain data as a singleton object. that singleton object can be accessed from anywhere by retaining the data within different threads. You atomic property, if you want to make thread safe reading.
You can do it using dispatch_queue aka Grand Central Dispatch (GCD) , Refer ConcurrencyProgrammingGuide.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Background work
//Here you will get Object A , lets say Object A as `UIImage`
//For example
NSURL *imageURL = [NSURL URLWithString:#"http://example.com/1.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
dispatch_async(dispatch_get_main_queue(), ^{
// Update UI
UIImage *image = [UIImage imageWithData:imageData];
//Here, lets say UIImageView is Object B
//Now Pass Object A to Object B like as follow
self.imageView.image = self.image;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Background work
dispatch_async(dispatch_get_main_queue(), ^{
// Update UI
});
});
});
});
OR
Assume you have two threads, threadX and threadY, each with one object, objectA and objectB. Do something like:
char buffer[100] = "Hello";
NSData *data = [NSData dataWithBytes:byBuffer length:100];
[objectY performSelector:#selector(haveSomeData:) onThread:threadY withObject:data waitUntilDone:NO modes:[NSArray arrayWithObject:NSDefaultRunLoopMode]];

Related Links

How to know whether a Realm object was successfully updated?
Remove quality losing, after scale applied
Wake App from background mode on BLE interaction
registerNib UiTableView not working after project corruption
sql statement for inserting data in database for current month to next 2 months
How to authenticate the server with self signed certificate without using delegate methods in ios
UITableViewAutomaticDimension and multiline label issue
Socket.io performance on a simulated Very Bad Connection
Setting font requires size that overrides sizeToFit() Swift
Detecting time range in ios using Swift [closed]
How to know user clicked “Not Allow” button when iOS ask user to grant permission to access camera or Library? [duplicate]
Swift 2, UIView position and UITextField event
AudioConverterFillComplexBuffer return 561015652(!pkd)?
my iOS apps not showing in Brazil App Store
Autolayout Parent view height change
React native UIExplorer example not building on device

Categories

HOME
validation
performance
asp.net-identity
jsp
performance-testing
prometheus
wikipedia-api
windows-server-2012-r2
apache-spark-sql
autofac
librsvg
proftpd
graphengine
frequency
signature
yii2-advanced-app
googletest
helpers
servlet-filters
multiprocessing
django-autocomplete-light
ratchet-2
stylesheet
code-signing
desktop-app-converter
scom
petrel
background-process
dashboard-designer
mindstorms
procobol
firedac
essbase
subscription
using
phasset
ase
oracle-aq
requirements-management
glfw
typesetting
python-module
singlepage
asp.net-mvc-custom-filter
masspay
lapply
hapi.js
olingo
texture2d
rsh
geneos
submission
intrinsics
accounts
delphi-10-seattle
cng
realm-list
boost-coroutine
android-print-framework
fastparse
generalization
adserver
razor-2
django-postgresql
private-methods
fig
wmv
hebrew
array-address
pdf-scraping
ducttape
libtorrent-rasterbar
apache-commons-dbcp
external-dependencies
pubdate
nspersistentdocument
jmdns
timespan
harvest
ad-hoc-distribution
media-manager
quilt
ms-access-97
ruby-1.9.2
payment-services
springboard
blackberry-os-v5
xmltextwriter
plinqo
etrade-api
pinging
net-ssh
geneva-framework
templatebinding

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App